51

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.

Martin08
  • 20,990
  • 22
  • 84
  • 93

5 Answers5

115

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.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 1
    As a side note: it's better to use `/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. – David Wolever Feb 14 '11 at 15:05
  • Well, strictly speaking, `env` could be located elsewhere as well. But yes, that's much less propable than running the script on some other distro that happens to put python somewhere else. –  Feb 14 '11 at 16:29
  • That's true. But I don't know of any other, more portable, way of doing it. – David Wolever Feb 14 '11 at 17:10
  • 2
    Another note: 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. – Homer6 Nov 13 '12 at 18:35
  • On gvim for windows, I think `:set ff=unix` then saving fixes this issue. – KitsuneYMG Jun 12 '13 at 15:55
  • on Mac I changed the shebang to `#!/usr/bin/python3` and it worked – Json Jan 01 '23 at 19:08
7

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'
        ]
    }
)
djvg
  • 11,722
  • 5
  • 72
  • 103
mgc
  • 5,223
  • 1
  • 24
  • 37
3
  1. Add a line at the top of your script:

    #! /usr/bin/env python
    
  2. Rename your script from script_name.py to script_name
  3. 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
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • 5
    In addition, you need to add the location of the script to the PATH variable. otherwise you will need to execute it as ./script_name – Mukherjee Sep 11 '16 at 07:39
  • 1
    why did you suggest changing the name of the file? is there any repercussions of not doing that? – Vishal Singh Aug 12 '20 at 15:56
  • @VishalSingh The repercussion is that you have to type three extra characters to invoke the script, and for other it `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. – Anthon Aug 12 '20 at 19:11
  • Also, to paraphrase Charles Duffy, we don't use `ls.elf` or `emacs.bin` so why should your own commands have an extension? – tripleee Oct 26 '20 at 06:37
3

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Tassos Pan
  • 23
  • 5
1

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
darioo
  • 46,442
  • 10
  • 75
  • 103
Anto Binish Kaspar
  • 1,322
  • 1
  • 14
  • 20