Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run python script without typing 'python ...'

Tags:

python

bash

shell

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.

like image 347
Martin08 Avatar asked Feb 14 '11 14:02

Martin08


People also ask

Can we run py file without python?

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.

How do I run a Python script directly?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


4 Answers

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.

like image 86
David Wolever Avatar answered Oct 09 '22 09:10

David Wolever


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'
        ]
    }
)
like image 43
mgc Avatar answered Oct 09 '22 07:10

mgc


  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
like image 3
Anthon Avatar answered Oct 09 '22 07:10

Anthon


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.

like image 2
Tassos Pan Avatar answered Oct 09 '22 08:10

Tassos Pan