I want to run a Python script without explicitly having to call python every time in my shell.
I've tried to add the shebang #!/path/to/python, but this does not seem to work. Does anyone know a work around this?
Many thanks.
-
"this does not seem to work." The question does not seem to have a specific error message. Without the specific error, we don't seem to be able to guess what went wrong.S.Lott– S.Lott2011年02月14日 16:01:53 +00:00Commented Feb 14, 2011 at 16:01
-
It's called the shebang #!/usr/bin/pythonRolf of Saxony– Rolf of Saxony2016年09月11日 07:27:04 +00:00Commented Sep 11, 2016 at 7:27
-
Also 'chmod a+x' the file, and add the directory to the PATHMukherjee– Mukherjee2016年09月11日 07:42:30 +00:00Commented Sep 11, 2016 at 7:42
-
Which is your DE?Jacob Vlijm– Jacob Vlijm2016年09月11日 08:40:13 +00:00Commented Sep 11, 2016 at 8:40
-
Could somebody please explain what the PATH is? And how I can add my directory to it?St4rb0y– St4rb0y2016年09月12日 08:50:09 +00:00Commented Sep 12, 2016 at 8:50
5 Answers 5
You've got to add the shebang:
#!/usr/bin/env python
Then make the script executable:
chmod +x foo
Then you can run it like any other executable:
./foo
And a note from Homer6: if you're editing the file from windows and invoking it on linux, you may run into the cryptic "No such file or directory" error. It's due to the line endings of the lines being CRLF instead of LF. If you convert them to LF, the script will execute as expected. Notepad++> View> Show Symbols> Show End of Line to show the EOL characters. And Notepad++> Edit> EOL Conversion> Unix Format to convert all line endings to use LF. Alternatively, you can use the dos2unix tool (dos2unix foo.py), which is present on most Linux systems.
6 Comments
/usr/bin/env than the path to Python in the shebang line: if you hard-code a path, the script will stop working if Python is ever moved... But so long as Python is on $PATH, /usr/bin/env will work.:set ff=unix then saving fixes this issue.It didn't really apply to your personal scripts but as you are quoting beets, note that it is also possible to automate this action when you are distributing your packages, thanks to setuptools entry_point option.
So if you are distributing a package like myModule and want to make the main_function() function accessible via typing mymodulescript in the console you would probably add something like this to your setup.py file :
setup(
# your other arguments ..
entry_points={
'console_scripts': [
'mymodulescript = myModule:main_function'
]
}
)
Comments
Add a line at the top of your script:
#! /usr/bin/env python- Rename your script from
script_name.pytoscript_name - make the script executable:
chmod +x script_name
The line at the top selects the same python that you get when typing python at the prompt. You can also specify a direct path:
#!/opt/python/3.6/bin/python
4 Comments
script_name (which should reflect the function it performs) less clear. There are primitive OS-es that need such extensions to determine which interpreter to start the Unix/Linux and their shells have long ago solved that in a better way. Leaving the extension makes you look inexperienced.ls.elf or emacs.bin so why should your own commands have an extension?Another workaround could be to use an alias defined in the .bashrc :
e.g. add the following line in your .bachrc file :
alias mypythonalias="python mypyrhonfile.py"
type in terminal :
source ~/.bashrc
and then you may simply type:
mypythonalias
to execute the python file.
Comments
Ensure you are able to run /path/to/python on your terminal. And make sure you have given execute permission for your python file. You can give permission for the file by
chmod +x mypythonfile.py