90

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?


See also: ./xx.py: line 1: import: command not found .

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ctrlz
  • 1,073
  • 1
  • 10
  • 12

5 Answers5

140
  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

tzaman
  • 46,925
  • 11
  • 90
  • 115
  • still cant run by my name 'square': -bash: /usr/bin/square: No such file or directory – ctrlz Dec 15 '14 at 23:23
  • My program is called square and i have exported the dir in PATH – ctrlz Dec 15 '14 at 23:23
  • 2
    @bingo14 What is the output of `echo $PATH`? And the absolute path to your script? – Ismail Badawi Dec 15 '14 at 23:29
  • It is: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Development/adt-bundle/sdk/platform-tools:/Development/adt-bundle/sdk/tools – ctrlz Dec 16 '14 at 20:51
  • And where does your script reside? – tzaman Dec 16 '14 at 21:27
  • 2
    So that needs to be added to your path, like I showed in point 3 in my answer. – tzaman Dec 16 '14 at 22:12
  • Doesn't work for scripts that have dependencies our other scripts + resources they require. – Destaq Jul 09 '20 at 10:09
  • If you get an error `env: python\r: No such file or directory` it's because the python script has windows line endings. You need to follow: https://stackoverflow.com/questions/19425857/env-python-r-no-such-file-or-directory – Will Sheppard Dec 02 '22 at 09:13
81

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.

merrydeath
  • 1,009
  • 7
  • 6
  • Is it possible to call entry points like `myscript update` or only `myscript_update` – Nikita Prokaiev Aug 14 '21 at 20:04
  • 1
    @NikitaProkaiev In `myscript update`, `update` is a command line argument to the script -- the same as if you ran `python myscript.py update`. It would have no effect unless you wrote code in your script to handle command line arguments. You could define another entry in `console_scripts` like `myscript_update=myscript:update` to call, in this case, a function called `update`. – cosmicFluke Sep 13 '21 at 20:19
  • if you just have a python script then do `pip install -e .` – Karishma Sukhwani Sep 08 '22 at 11:46
11

I usually do in the script:

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

And in terminal:

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

permission table

anothernode
  • 5,100
  • 13
  • 43
  • 62
dAn
  • 517
  • 1
  • 6
  • 10
3

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.

mallwright
  • 1,670
  • 14
  • 31
2

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

Ali Taheri
  • 116
  • 3