Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run a python script in terminal without the python command

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 ?

like image 638
Alpagut Avatar asked Mar 23 '13 14:03

Alpagut


People also ask

How do I run a .py file in terminal?

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!

Do you need Python to run .py files?

The only realistic way to run a script on Windows without installing Python, is to use py2exe to package it into an executable. Py2exe in turn examines your script, and embeds the proper modules and a python interpreter to run it.


2 Answers

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 
like image 146
Martijn Pieters Avatar answered Sep 20 '22 19:09

Martijn Pieters


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

like image 45
arajek Avatar answered Sep 21 '22 19:09

arajek