Table of Contents
You have seen how you can reuse code in your program by defining functions once. What if you wanted to reuse a number of functions in other programs that you write? As you might have guessed, the answer is modules. A module is basically a file containing all your functions and variables that you have defined. The filename of the module must have a .py extension.
A module can be imported by another program to make use of its functionality. This is how we use the Python standard library as well. First, we will see how to use the standard library modules.
Example 8.1. Using the sys module
#!/usr/bin/python # Filename : using_sys.py import sys print 'The command line arguments used are:' for i in sys.argv: print i print '\n\nThe PYTHONPATH is', sys.path, '\n'
$ python using_sys.py we are arguments The command line arguments used are: using_sys.py we are arguments The PYTHONPATH is ['', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', '/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', '/usr/lib/python2.2/site-packages', '/usr/lib/python2.2/site-packages/gtk-2.0']
First, we import a module using the import statement. Here, we import the sys module which contains some functionality related to the Python interpreter and its environment. When Python comes to the import sys statement, it looks for the file sys.py in one of the directories listed in the sys.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 the sys module is referred to using the notation sys.argv. One of the advantages of this approach is that it does not clash with any argv variable declared in our program. Also, it indicates that this variable belongs to the sys module and has not been defined in our module.
The sys.argv variable is a list of strings. We will learn more about lists in later chapters. What you need to know right now is that a list contains an ordered collection of items. The sys.argv variable contains the list of command line arguments i.e. arguments passed to the your program using the command line. Be especially careful to pass command line arguments as shown in the above output if you are using an IDE.
In this case, when we execute python using_sys.py we are arguments, we are executing the program using_sys.py with the python command. The other things are the arguments stored in the sys.argv variable. Remember, the name of the script running is always the first argument in the sys.argv. 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' as sys.argv[3].
We then use the for..in loop to iterate over this list and we print each argument.
The sys.path contains the list of directory names where modules are imported from. Observe that the first string in sys.path is empty - this empty string indicates the current directory which is also part of the sys.path (this is same as the PYTHONPATH 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 in sys.path.