The Standard Library provides a lot of features that you don’t get with IronPython. Many of these features you can use while developing IronPython applications. The following sections describe how to use each of the features. In addition, you’ll see how to use the Standard Library to create an application. Before you use this section, you must install the Python Standard Library and determine how you plan to access it from IronPython. Because the Python command line console is essentially the same as the IronPython command line console, you won’t find it in the sections that follow.
Using the Module Documentation
The module documentation provides terse information about each of the modules provided in the Standard Library. However, the information you receive is normally enough to use the module. To start this feature, choose Start ➪ Programs ➪ Python 2.6 ➪ Module Docs. You’ll see the pydoc dialog box shown in Figure 6-3. This dialog box already has a search term entered into it.
When you enter a search term and press Enter, Module Docs searches the documentation to locate all of the matching entries. To use a particular search result, highlight its entry in the list and click Go To Selected. Your browser will open and show a search topic similar to the one shown in Figure 6-4.
You might want to do a little exploring, rather than enter a specific search term. In this case, click Open Browser on the pydoc dialog box to go to the top-level Module Docs page shown in Figure 6-5. Simply click the links to drill down to the search topic you want.
After you find all of the information you want, click Quit Serving on the pydoc dialog box. Module Docs will shut down. Because Module Docs runs on an internal Web server, you’ll want to shut Module Docs down when your search is complete to save processing cycles.
Using the Python Manuals
The Python Manuals are a full-fledged compiled help module, which is the same kind of help you’ve been using in Windows for just about every other application you own. To access this feature, choose Start ➪ Programs ➪ Python 2.6 ➪ Python Manuals. You’ll see the Python 2.6.4 Documentation window shown in Figure 6-6.
As you can see, the Python Manual relies on a standard help file. You can search it, look through the table of contents, click links, and do everything else you’d do with a standard help file.
One potential issue with using the Python Manuals is that the help is Python-specific. This help file will contain information that won’t apply to IronPython in some cases because IronPython doesn’t precisely implement the full Python feature set. Consequently, you need to test any help you receive before you accept it as completely useful.
Working with IDLE
IDLE is the IDE that IronPython should have shipped with because it lets you perform all kinds of interesting tasks using Python. To access this feature, choose Start ➪ Programs ➪ Python 2.6 ➪ IDLE (Python GUI). You’ll see a window similar to the one shown in Figure 6-7.
The window opens immediately with a fully functional interpreter by default. The interpreter is called the Shell Window. For example, in Figure 6-7, if you type 2 + 2 and press Enter, you’ll see the answer 4 on the next line. However, it’s the next feature that you’ll like best. When you type a method, such as help, you see an IntelliSense-like help feature, as shown in the figure.
IDLE also has a full-fledged editor, called the Editor Window. The Editor Window contains all of the editing features that you’d expect in any IDE. You use the Editor Window to edit your code and the Shell Window to interact with the Python interpreter and test your code using the debugger.
Of course, IDLE has a lot more to offer you, including a full-fledged debugger and a number of editing tools. Most important, you can open and save files quite easily using IDLE. You can even track down modules by exploring the paths accessible using sys.path. The following sections tell you considerably more about this interesting IDE.
IDLE has a number of configuration options that you can set. These options make it possible to customize IDLE to an extent. For example, you can choose a theme for coloring the editor, if desired. Choose Options ➪ Configure IDLE to display the dialog box shown in Figure 6-8.
The Font/Tabs page contains a number of interesting options. For example, if you’re a bit older, you might want to use a larger font than the default or even a font that offers serifs to make it easier to follow text on screen. You can also make the text bold, which can help when you’re using a laptop in a sunny environment. The tab setting helps you use indentation, but still get more text on screen. Most people find that 3 spaces is the minimum indentation that works.
The Highlighting page, shown in Figure 6-9, helps you adjust the coloration used to display certain kinds of code and application features. This is an especially useful feature for people who have color blindness because you can adjust the colors to make various kinds of highlights easier to see. You can save your settings as a theme. In fact, you might want to have special themes for particular purposes, such as low ambient lighting.
The Keys page, shown in Figure 6-10, helps you set the control keys used to move around the application quickly. IDLE comes with four key setups as a default.
- IDLE Classic Mac
- IDLE Classic OS/X
- IDLE Classic Unix
- IDLE Classic Windows
Of course, you can always create your own key setups and save them. Some people (such as those who are left-handed) might find that the default key setups don’t work for them.
The General page, shown in Figure 6-11, lets you set general application settings. For example, you can tell IDLE what to do during certain events such as startup and before you run your application. You can also control the initial size of the IDLE window and the encoding used for the text.
The item of special interest on the General page is the Additional Help Sources list at the bottom of the page. To add a new help source, click Add. You’ll see the New Help Source dialog box shown in Figure 6-12. Notice that you can use either a local help source or a remote help URL. IronPython developers have good reason to add other help sources they can use for IronPython-specific needs.
IronPython doesn’t provide a lot of specific help sources, but it does provide some. The following list provides some suggestions of URLs you might want to add to your copy of IDLE.
- Differences between IronPython 2.0.x and CPython 2.5.2: http://ironpython.codeplex.com/ wikipage?title=IPy2.0.xCPyDifferences
- IronPython Readme: /Program Files/IronPython 2.6/ Readme.html
- IronPython Tutorial: /Program Files/IronPython 2.6/ Tutorial/Tutorial.htm
- IronPython Samples: http://ironpython.codeplex.com/ wikipage?title=Samples
- More Information About IronPython: http://ironpython .codeplex.com/wikipage?title=More Information
Performing an Interactive Edit
To use IDLE as an editor, you simply begin entering commands in the Shell Window. If you have IDLE configured to open the Editor Window first, choose Run ➪ Python Shell to open the Shell Window. The interpreter will tell you the result of each command you enter, so you know immediately whether the command will work. After you’ve completed the task you want to perform, choose File ➪ Save and save the file. At this point, IDLE will save everything you typed, some of which isn’t useful for an application, so you need to edit the code.
If you haven’t already configured IDLE to open the Editor Window first, do so now. Choose Options ➪ Configure IDLE, select Open Edit Window in the At Startup section on the General tab, and click OK. Choose File ➪ New Window. IDLE will open an Editor Window that you can use to edit the code you just created interactively.
In the Editor Window, choose File ➪ Open. You’ll see an Open dialog box like the one shown in Figure 6-13. Select the file you just saved and click Open. IDLE opens the file you saved from your interpreter section.
Figure 6-14 shows a short example of an interpreter session saved as a file. As you can see, you need to perform a number of edits, such as removing the initial startup information. Modify the file just as you would using any other editor and save the result. The Edit menu contains a full list of editing tools, including the familiar Cut, Copy, and Paste commands, as well as a Find command. It even includes a command to go directly to a specific line in the file, Edit ➪ Go To Line, which comes in handy for dealing with interpreter error messages.
In this case, you can cut the text shown in Figure 6-14 down to three simple lines.
sys.path.append(‘C:\0255 – Source Code\Chapter06’)
After you make the changes, you can always reload the file into the interpreter and debug it or work with it in other ways. For example, if you want to run the application, press F5 or choose Run ➪ Run Module.
Of course, you can always build an application from scratch using the Editor Window. You get the same IntelliSense-type help as you do when using the interpreter in the Shell Window. Unfortunately, when using the Editor Window directly, you don’t get instant feedback, but you do benefit from not having to edit the result.
Using the Path Browser
IDLE provides more than one way to locate a file you want to edit. One of these methods is to display a dialog box that contains a hierarchical display of the current sys.path content. You drill down through the list to find the file you want to work with. Not only does this technique make it easy to find what you want, but it also tells you that the interpreter can see the file. To use this feature, choose File ➪ Path Browser. You’ll see the Path Browser dialog box shown in Figure 6-15.
Notice that the Path Browser shows which folders are packages and clearly shows the files. Doubleclick any file entry to open it. The file will open in a separate copy of IDLE, so you don’t need to worry about modifications to your current session.
Using the Class Browser
Class Browser works with the currently loaded file. It helps you see the elements within the file and go directly to them. To see the class setup for your application, choose File ➪ Class Browser. You’ll see a dialog similar to the one shown in Figure 6-16, except that it contains specifics for your application.
Using the Debugger
The IDLE debugger is interesting. You use it to debug your application, but not in the same way as you would within Visual Studio.
In order to make the debugger useful, choose Run ➪ Python Shell in the Editor Window. IDLE starts a copy of the Shell Window. In the Shell Window, choose Debug ➪ Debugger. You’ll see the strange Debug Control dialog box shown in Figure 6-17 open. At this point, you’re ready to debug your application.
To start debugging the application, choose Run ➪ Run Module or press F5 in the Editor Window (not the Shell Window). IDLE actually picks the Shell Window that you opened earlier and stops execution immediately so you can see what’s happening with your application, as shown in Figure 6-18.
At this point, you can click Step within the Debug Control to go to the next line of execution, Over to skip over a line of execution (the commands still execute, but you won’t see them), or Out to move out of a function or method. If you click Go, the application will continue to execute without debugging support. Clicking Quit ends the application.
The IDLE debugger has no concept of breakpoints. You have no immediate window for entering commands or watch windows to track expressions. Even so, the debugger does work well, even if it does take a while for the .NET developer to get used to it.
Looking for Standard Library Issues
It’s important to exercise care when working with the Standard Library. Sometimes, it appears that IronPython provides support for a feature when it really doesn’t. For example, Listing 6-1 shows an application that runs fine within IDLE because IDLE relies on the Python interpreter, not the IronPython interpreter.
Listi ng 6-1: An example of a Standard Library application
from Tkinter import *
# Represents the main window of an application.
root = Tk()
“Defines a basic test of IDLE”
“Display the IDLE About dialog box”
# Make sure you destroy the window when you get done with it.
“A very basic function in Python”
# Set the path to include the application directory.
sys.path.append(‘C:\0255 – Source Code\Chapter06’)
# Call SayHello()
# Display the IDLE About dialog box.
MyAbout = TestIDLE()
This is an interesting example because it displays the IDLE About dialog box. When you run this example, the code begins by doing something simple — it modifies the path to point to the example directory and then it displays a simple hello message to the user.
The problem occurs with the second part of the example. The process is relatively straightforward. The TestIDLE class contains a method called ShowAbout(). ShowAbout() creates a window and then displays the IDLE About dialog box in it. The code then destroys the window it created and ends.
When you try to run this example using IronPython by typing IPY IDLETest.py at the command prompt and pressing Enter, the first message you see says that IronPython knows nothing about a Tkinter module, which provides access to the windowing environment provided by Tk. You can read all about Tkinter at http://www.pythonware.com/library/tkinter/introduction/. The bottom line is that Tkinter is a windowing environment that many Python developers use, so you’re going to run into it when working with IronPython.
At this point, you notice that IronPython does indeed include support for Tkinter in the Program FilesIronPython 2.6Liblib-tk folder. So you add this directory to your IRONPYTHONPATH environment variable by typing Set IRONPYTHONPATH=%IRONPYTHONPATH %;C:Program FilesIronPython 2.6Liblib-tk and pressing Enter. This command adds the required directory to your application environment, so you type IPY IDLETest.py again and press Enter. Now you see an error message, “ImportError: No module named _tkinter.”
Unfortunately, you’ll never find a _tkinter module in the IronPython folders. You’ll find it in the Python26DLLs directory as _tkinter.pyd and in the Python26libs directory as _tkinter .lib. The alarms should be going off in your head at this moment. Python implements Tkinter as a C library, which means that IronPython doesn’t support it, despite the fact that IronPython includes the required Tkinter.py file. The point of this whole exercise is that you’re going to run into some very popular Python features that simply won’t work in IronPython because they require C support (despite the fact that it appears that IronPython does support it). Your only choice (in most cases) is to avoid using the module or rely on straight Python instead. Happily, some developers are working on the Tkinter problem. You can read about one such solution at http://www.voidspace.org .uk/ironpython/cpython_extensions.shtml.