49

I have a python script let's name it script1.py. I can run it in the terminal this way:

python /path/script1.py ... 

but I want to run like a command-line program:

arbitraryname ... 

how can i do it ?

0

    4 Answers 4

    84

    You use a shebang line at the start of your script:

    #!/usr/bin/env python 

    make the file executable:

    chmod +x arbitraryname 

    and put it in a directory on your PATH (can be a symlink):

    cd ~/bin/ ln -s ~/some/path/to/myscript/arbitraryname 
    4
    • the cd ~/bin/ is correct? shouldn't it be cd ~bin/ without the first slash?
      – Arcones
      CommentedMay 16, 2018 at 7:47
    • 1
      Absolutely. ~bin/ is something completely different, that’s the home directory of the user named bin, while ~/bin/ is the bin subdirectory of the home directory of the current user. The latter is a common per-user directory to add to the PATH environment variable.CommentedMay 16, 2018 at 8:16
    • great and detailed answer. however, I don't get why I am having this: ➜ ~ cd ~bin ➜ ~bin pwd /bin ➜ ~bin whoami arcones I got the same in two different linux machines, any idea why?
      – Arcones
      CommentedMay 16, 2018 at 17:02
    • 1
      @Arcones: ~username doesn't have anything to do with the current user, so the whoami output is irrelevant. Most Linux systems have a bin user, whose homedirectory is set to /bin, see the Linux Standards Base.CommentedMay 16, 2018 at 17:23
    50

    There are three parts:

    1. Add a 'shebang' at the top of your script which tells how to execute your script
    2. Give the script 'run' permissions.
    3. Make the script in your PATH so you can run it from anywhere.

    Adding a shebang

    You need to add a shebang at the top of your script so the shell knows which interpreter to use when parsing your script. It is generally:

    #!path/to/interpretter 

    To find the path to your python interpretter on your machine you can run the command:

    which python 

    This will search your PATH to find the location of your python executable. It should come back with a absolute path which you can then use to form your shebang. Make sure your shebang is at the top of your python script:

    #!/usr/bin/python 

    Run Permissions

    You have to mark your script with run permissions so that your shell knows you want to actually execute it when you try to use it as a command. To do this you can run this command:

    chmod +x myscript.py 

    Add the script to your path

    The PATH environment variable is an ordered list of directories that your shell will search when looking for a command you are trying to run. So if you want your python script to be a command you can run from anywhere then it needs to be in your PATH. You can see the contents of your path running the command:

    echo $PATH 

    This will print out a long line of text, where each directory is seperated by a semicolon. Whenever you are wondering where the actual location of an executable that you are running from your PATH, you can find it by running the command:

    which <commandname> 

    Now you have two options: Add your script to a directory already in your PATH, or add a new directory to your PATH. I usually create a directory in my user home directory and then add it the PATH. To add things to your path you can run the command:

    export PATH=/my/directory/with/pythonscript:$PATH 

    Now you should be able to run your python script as a command anywhere. BUT! if you close the shell window and open a new one, the new one won't remember the change you just made to your PATH. So if you want this change to be saved then you need to add that command at the bottom of your .bashrc or .bash_profile

      12

      Add the following line to the beginning script1.py

      #!/usr/bin/env python 

      and then make the script executable:

      $ chmod +x script1.py 

      If the script resides in a directory that appears in your PATH variable, you can simply type

      $ script1.py 

      Otherwise, you'll need to provide the full path (either absolute or relative). This includes the current working directory, which should not be in your PATH.

      $ ./script1.py 
      2
      • 1
        I want to run it from anywhere without the full path. How can I add it to this PATH variable ?
        – Alpagut
        CommentedMar 23, 2013 at 14:34
      • In your .bash_profile (I'm assuming you use bash), add the following line: PATH+=":/path/where/script1.py/lives", adding the appropriate directory, of course.
        – chepner
        CommentedMar 23, 2013 at 14:35
      2

      You need to use a hashbang. Add it to the first line of your python script.

      #! <full path of python interpreter> 

      Then change the file permissions, and add the executing permission.

      chmod +x <filename> 

      And finally execute it using

      ./<filename> 

      If its in the current directory,

      0

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.