Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Using the sys module

Example 8.1. Using the sys module

#!/usr/bin/python
# Filename: using_sys.py import sys print 'The command line arguments are:' for i in sys.argv:
print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'

Output

$ python using_sys.py we are arguments The command line arguments are: using_sys.py
we
are
arguments

The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']

How It Works

First, we import thesys module using theimport statement. Basically, this translates to us telling Python that we want to use this module. Thesys module contains functionality related to the Python interpreter and its environment.

When Python executes the import sys statement, it looks for thesys.py module in one of the directores listed in itssys.path variable. If the file is found, then the statements in the main block of that module is run and then the module is made available for you to use. Note that the initialization is done only the first time that we import a module. Also, 'sys' is short for 'system'.

The argv variable in thesys module is referred to using the dotted notation sys.argv - one of the advantages of this approach is that the name does not clash with anyargv variable used in your program. Also, it indicates clearly that this name is part of thesys module.

The sys.argv variable is a list of strings (lists are explained in detail in later sections). Specifically, thesys.argv contains the list of command line arguments i.e. the arguments passed to your program using the command line.

If you are using an IDE to write and run these programs, look for a way to specify command line arguments to the program in the menus.

Here, when we execute python using_sys.py we are arguments, we run the moduleusing_sys.py with the python command and the other things that follow are arguments passed to the program. Python stores it in thesys.argv variable for us.

Remember, the name of the script running is always the first argument in the sys.argv list. So, in this case we will have 'using_sys.py' as sys.argv[0], 'we' as sys.argv[1], 'are' as sys.argv[2] and'arguments' assys.argv[3] . Notice that Python starts counting from 0 and not 1.

The sys.path contains the list of directory names where modules are imported from. Observe that the first string insys.path is empty - this empty string indicates that the current directory is also part of thesys.path which is same as thePYTHONPATH environment variable. This means that you can directly import modules located in the current directory. Otherwise, you will have to place your module in one of the directories listed insys.path .