Brief introduction
You've learned how to define a function in your program and reuse the code. If you want to reuse many functions in other programs, how do you write programs? As you may have guessed, the answer is to use a module. A module is basically a file that contains all of the functions and variables you define. In order to reuse a module in another program, the module's filename must be extended with. py.
A module can be entered from another program to take advantage of its functionality. This is also how we use the Python standard library. First, we'll learn how to use the standard library module.
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'
(source file: code/using_sys.py)
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 use the import statement to enter the SYS module. Basically, this statement tells Python that we want to use this module. The SYS module contains functions related to the Python interpreter and its environment.
When Python executes the import SYS statement, it looks for the sys.py module in the directory listed in the Sys.path variable. If this file is found, the statement in the main block of the module will be run, and the module will be able to be used by you. Note that the initialization process takes place only the first time we enter the module. In addition, "SYS" is the abbreviation for "system".
The argv variable in the SYS module, by using the dot number to indicate--sys.argv--, has the advantage that the name does not conflict with any argv variables that are used in your program. In addition, it is clear that this name is part of the SYS module.
The SYS.ARGV variable is a list of strings (the list is explained in detail in later chapters). Specifically, SYS.ARGV contains a list of command-line arguments, that is, the arguments that are passed to your program using the command line.
If you are writing to run these programs using the IDE, look for a way to specify the command-line arguments for the program in the menu.
Here, when we execute the Python using_sys.py we are arguments, we run the using_sys.py module using the Python command, followed by the contents being passed as arguments to the program. Python stores it in SYS.ARGV variables for us.
Remember that the name of the script always sys.argv the first parameter of the list. So, here, ' using_sys.py ' is sys.argv[0], ' we ' are sys.argv[1, ' are ' is sys.argv[2 ' and ' arguments ' is sys.argv[3. Note that Python counts starting at 0, not starting at 1.
Sys.path contains the list of directory names for the input module. We can observe that the first string of Sys.path is empty--this empty string indicates that the current directory is also part of the Sys.path, which is the same as the PYTHONPATH environment variable. This means that you can directly enter the module in the current directory. Otherwise, you have to put your module in one of the directories listed in Sys.path.