How do I make a python script executable?

PythonCommand Line

Python Problem Overview


How can I run a python script with my own command line name like 'myscript' without having to do 'python myscript.py' in the terminal?

Python Solutions


Solution 1 - Python

  1. Add a shebang line to the top of the script:

    #!/usr/bin/env python

  2. Mark the script as executable:

    chmod +x myscript.py

  3. Add the dir containing it to your PATH variable. (If you want it to stick, you'll have to do this in .bashrc or .bash_profile in your home dir.)

    export PATH=/path/to/script:$PATH

Solution 2 - Python

The best way, which is cross-platform, is to create setup.py, define an entry point in it and install with pip.

Say you have the following contents of myscript.py:

def run():
    print('Hello world')

Then you add setup.py with the following:

from setuptools import setup
setup(
    name='myscript',
    version='0.0.1',
    entry_points={
        'console_scripts': [
            'myscript=myscript:run'
        ]
    }
)

Entry point format is terminal_command_name=python_script_name:main_method_name

Finally install with the following command.

pip install -e /path/to/script/folder

-e stands for editable, meaning you'll be able to work on the script and invoke the latest version without need to reinstall

After that you can run myscript from any directory.

Solution 3 - Python

I usually do in the script:

#!/usr/bin/python
... code ...

And in terminal:

$: chmod 755 yourfile.py
$: ./yourfile.py

permission table

Solution 4 - Python

Another related solution which some people may be interested in. One can also directly embed the contents of myscript.py into your .bashrc file on Linux (should also work for MacOS I think)

For example, I have the following function defined in my .bashrc for dumping Python pickles to the terminal, note that the ${1} is the first argument following the function name:

depickle() {
python << EOPYTHON
import pickle
f = open('${1}', 'rb')
while True:
   try:
      print(pickle.load(f))
   except EOFError:
      break
EOPYTHON
}

With this in place (and after reloading .bashrc), I can now run depickle a.pickle from any terminal or directory on my computer.

Solution 5 - Python

The simplest way that comes to my mind is to use "pyinstaller".

  1. create an environment that contains all the lib you have used in your code.
  2. activate the environment and in the command window write pip install pyinstaller
  3. Use the command window to open the main directory that codes maincode.py is located.
  4. remember to keep the environment active and write pyinstaller maincode.py
  5. Check the folder named "build" and you will find the executable file.

I hope that this solution helps you. GL

Solution 6 - Python

I've struggled for a few days with the problem of not finding the command py -3 or any other related to pylauncher command if script was running by service created using Nssm tool.
But same commands worked when run directly from cmd.
What was the solution? Just to re-run Python installer and at the very end click the option to disable path length limit.
I'll just leave it here, so that anyone can use this answer and find it helpful.
enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionctrlzView Question on Stackoverflow
Solution 1 - PythontzamanView Answer on Stackoverflow
Solution 2 - PythonmerrydeathView Answer on Stackoverflow
Solution 3 - PythondAnView Answer on Stackoverflow
Solution 4 - PythonmallwrightView Answer on Stackoverflow
Solution 5 - PythonAli TaheriView Answer on Stackoverflow
Solution 6 - PythonJagoda GorusView Answer on Stackoverflow