I'm using Debian Linux.
I was wondering if there was a way to configure to run all Python scripts in the terminal by typing script.py (instead of python script.py).
5 Answers
There are two things you need to do:
- Make sure the file is executable:
chmod +x script.py Use a shebang to let the kernel know what interpreter to use. The top line of the script should read:
#!/usr/bin/pythonThis assumes that your script will run with the default python. If you need a specific version, just specify in the shebang:
#!/usr/bin/python2.7
Now you can type:
./script.pyif the script is in your current directory, or:
script.pyif the location of the script happens to be in your PATH, or:
path/to/script.pyotherwise.
8Under linux you can simply use the hashbang(aka shebang). Add the line
#!/usr/bin/pythonif you want to execute the default python interpreter.
#!/path/to/python[x.x]to use some specific version, or
#!/usr/bin/env pythonIf you want the environment to find python for you.
You will also be required to make the script executable
chmod +x script[.py] 0 Use:
#!/usr/bin/env pythonThis will ensure that the python the user expects to be used will be the one that runs the script. This is especially important if the user is using virtualenv to have a specific version of python in a given environment.
Use a shebang line at top of your script as below:
!/usr/bin/python
Update appropriate python version on which you want to execute the script. e.g. For python 3.6 its
!/usr/bin/python3.6
for default interpreter
!/usr/bin/python
Note: Make sure script has executable permission.
Add the following line to the top of the script to run default python interpreter:
#!/usr/bin/pythonTo specify the python version then add the version number after python:
#!/usr/bin/python2.7If you have a local build of python and would like to use that:
#!/usr/bin/env pythonThen you will need to make your script executable by running the following command:
chmod +x script.pyTo run the script:
./script.py