At this point, you know how to create basic functions; determine which modules, objects, and functions are available for use; and import external modules as needed. The last major basic task you need to know about is interacting with the IronPython environment. Knowing this information will help you create better applications. The following sections provide an overview of many environmental needs you’ll have when using IronPython.
Obtaining Version Information
You may find a need to programmatically process the version information for a particular IronPython installation. Perhaps your application requires a newer version of IronPython. You can obtain this information programmatically using the sys.version attribute. The following code shows a simple method for checking the IronPython version.
import sys
print sys.version
Of course, your concern may revolve around Windows. In this case, you can use sys.getwindowsversion() to obtain the information you need. The output is an array containing the following five values:
- Major version
- Minor version
- Build
- Platform
- String describing installation (such as the current service pack)
Changing sys.path Values
The sys.path attribute is an array containing paths to various parts of IronPython. You can use sys.path to locate or modify the path for IronPython. For example, if you type print sys .path[0] and press Enter, you obtain the path for the currently executing application. The standard list provides these locations:
- Executing application path (blank if the interpreter can’t determine the location)
- IronPython library directory
- IronPython DLL directory
- IronPython executable directory
- IronPython site-packages directory
The interpreter always searches these paths, looking for any modules you want to import or other resources your application requires. You can add or remove entries as necessary.
Obtaining Command Line Arguments
An application can receive command line arguments when it executes and then processes those arguments, just as you would do with any other application. IronPython uses a similar approach to that of C and C++. You use the sys.argv array to obtain a list of the arguments passed at the command line. For example, if you type print sys.argv[0], you see the first command line argument passed to the application. The sys.argv array is blank when you start the interpreter without specifying a module to execute.
If you’re getting the idea that loading the sys module provides all kinds of power for your application, you’re right. You can see a complete list of the sys module functions and attributes at http://docs.python.org/library/sys.html. Of course, you’ll see additional examples as the book progresses.