Python 101 (part 7): Dinner With A Hungry Giant - Bucking The System (Page 9 of 9 ) A special mention should be made here of Python's "sys" module, which allows you to manipulate system-specific parameters like the list of currently-loaded modules and the module search path. The following example demonstrates the important attributes of this module:
>>> import sys
>>> # path to python binary
>>> sys.executable
'/usr/bin/python'
>>> # platform
>>> sys.platform
'linux-i386'
>>> # list of loaded modules
>>> sys.modules
{'os.path': ,
'os':
, 'readline': ,
'exceptions': , '__main__': , 'posix': , 'sys': , '__builtin__': , 'site':
, 'signal': , 'UserDict': , 'posixpath':
, 'stat':
}
>>>
The "path" attribute specifies the search path for modules, and can be modified using standard list constructs.
>>> sys.path
['', '/usr/lib/python1.5/', '/usr/lib/python1.5/plat-linux-i386',
'/usr/lib/python1.5/lib-tk', '/usr/lib/python1.5/lib-dynload']
>>> sys.path.append("/usr/local/pymod/")
>>> sys.path
['', '/usr/lib/python1.5/', '/usr/lib/python1.5/plat-linux-i386',
'/usr/lib/python1.5/lib-tk', '/usr/lib/python1.5/lib-dynload',
'/usr/local/pymod/']
>>>
The "argv" attribute contains arguments passed to the program on the command line. Consider the following program:
#!/usr/bin/python
import sys
print "Arguments:"
for x in sys.argv:
print x
Here's the output:
$ script.py medusa.server.com vikram 110
Arguments:
script.py
medusa.server.com
vikram
110
As you can see, the first element of the "argv" list contains the name of the called script, with the remaining elements holding the command-line arguments. Finally, the "__builtin__" module contains information on the various functions that are built into the interpreter. Take a look:
>>> import __builtin__
>>> dir()
['__builtin__', '__builtins__', '__doc__', '__name__']
>>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'EOFError',
'Ellipsis',
'EnvironmentError', 'Exception', 'FloatingPointError', 'IOError',
'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplementedError',
'OSError', 'OverflowError', 'RuntimeError', 'StandardError', 'SyntaxError',
'SystemError', 'SystemExit', 'TypeError',
'ValueError', 'ZeroDivisionError', '_', '__debug__', '__doc__',
'__import__', '__name__', 'abs', 'apply', 'buffer', 'callable', 'chr',
'cmp', 'coerce', 'compile', 'complex', 'delattr', 'dir', 'divmod', 'eval',
'execfile', 'exit', 'filter',
'float', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input',
'int', 'intern', 'isinstance', 'issubclass', 'len', 'list', 'locals',
'long', 'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'quit', 'range',
'raw_input', 'reduce',
'reload', 'repr', 'round', 'setattr', 'slice', 'str', 'tuple', 'type',
'vars', 'xrange']
>>>
The errors you see are built-in exceptions - we'll be discussing them in detail in the next article. More information on the various modules which ship with Python is available at http://www.python.org/doc/current/lib/lib.html And that's about it for this discussion of Python modules. In this article, you found out how to logically group functions together into modules, which are the highest-level abstraction in Python. You examined two different techniques for importing module functions and variables into your own programs, together with the implications of each on your program's namespace. Finally, you found out a little bit more about the default modules that ship with Python, hopefully saving yourself some time the next time you sit down to code a Python program In the next - and final - article of this series, I will be examining Python's error-handling routines, and demonstrating how to use them to trap and resolve program errors. Make sure that you come back for that one! Note: All examples in this article have been tested on Linux/i586 with Python 1.5.2. Examples are illustrative only, and are not meant for a production environment. YMMV! |
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|