Running Python Programs from the Command-line
Typical ways of running python programs are discussed on the Running Python page, including introduction to running from the command line. Here are some tips on more efficiently running python from the command line.
The -i option
While developing your program, it is almost always best to run python with the -i command line option:
python -i <script>
python -i hello.py
This will have two consequences:
Once python comes to the end of your script it will stay in the interpreter. This will allow you to check the values of variables or to call any functions again if requires.
If an error occurs, you will stay in the interpreter, allowing you to call the interactive python debugger:import pdb pdb.pm()
This allows you to inspect what went wrong.
Command line arguments
The arguments on the command line with which the python script was run are available in the sys.argv list:
#script cmdargs.py import sys print sys.argv
If you run as:
python cmdargs.py hello
You will see:
If you use command line arguments extensively then you should use the optparse module.
What are your options if you get tired of typing:
and so on, all the time? You can save yourself having the type the python part of the command by making the script hello.py executable and embedding within hello.py which program needs to be run to execute it. This is how to do it.
To make a file executable on unix you need to issue the following command:
chmod +x hello.py
This sets the execute bit meaning the file becomes executable. However, since the code within the file is plain python text rather than a real sequence of instructions to the computer processor, you need to tell the system a bit about how to execute the file. This is done by having a special first line in the file which starts with the characters #!. In this example we would have something like:
#!/usr/bin/python #This is hello.py # print "Hello!"
This command can now be run directly from the command line:
That is, there is no need to specify the python part of the command line.
There is in fact a discrepancy between the two ways of running hello.py presented in the last section. If you type:
Then the version of python that gets executed is whichever one is found first in your PATH environment variable. You can find out which python it is by typing:
However, if you follow the advice in the previous section and set the first line to #!/usr/bin/python, then the script is always going to be run with /usr/bin/python.
If this is not what is desired you can set the first line to:
This will reproduce the effect of typing of python on the command line, that is, it will execute the first python found in the directories listed in the PATH environment variable.
The command /usr/bin/env also allows you to change the environment of the program before it is executed. For example:
#!/usr/bin/env PYTHONPATH=/opt/webscripts/ python
Will allow the executed python code to load modules from the directory /opt/webscripts