The IronPython console is the best place to begin working with IronPython. You can enter a few statements, test them out, and then work out additional details without too many consequences. In addition, because the console is interactive, you obtain immediate feedback, so you don’t have to wait for a compile cycle to discover that something you’re doing is completely wrong. In fact, even after you’ve mastered IronPython, you’ll find that you use the console to try things out. Because IronPython is a dynamic language, you can try things without worrying about damaging an application. You can test things quickly using the console and then include them in your application. The following sections describe the IronPython console and how to use it.
Opening and Using the Default Console
The IronPython console is an application provided with the default installation. You access it using the Start ➪ Programs ➪ IronPython 2.6 ➪ IronPython Console command. The console, shown in Figure 1-3, looks something like a command prompt, but it isn’t.
Notice that the top of the window tells you which version of IronPython you’re using and which version of the .NET Framework it’s running on. This is important information because it helps you understand the IronPython environment and what limitations you have when working with IronPython. Below this first line, you’ll see some commands that Microsoft thought you might find useful. The “Getting Help with Any Function” section of the chapter tells you more about the Help command.
To use the console, simply type the commands you want to issue. When you’re done, IronPython will execute the commands and output any result you requested. A command need not be a function call or an object instantiation as it is in other languages. For example, type 2 + 2 right now and then press Enter. You’ll see the result of this simple command, as shown in Figure 1-4.
Whenever you want to end a particular task, such as working with Help, press Enter a second time. The console will take you to the previous level of interaction.
Getting Help with Any Function
You can get help with any function in the console. If you simply type help and press Enter in the console, IronPython tells you how to request interactive help or help about a specific object. To begin interactive help, type help() and press Enter. You’ll see the interactive help display shown in Figure 1-5.
Let’s say you have no idea of what you want to find. Console help provides you with a list of words you can type to get general help. These terms are:
Type any of these terms and press Enter. You’ll see a list of additional words you can type, as shown in Figure 1-6 for modules. Using this technique, you can drill down into help and locate anything you want. In fact, it’s a good idea to spend some time in help just to see what’s available. Even advanced developers can benefit from this approach — I personally follow this approach when I have time to increase my level of knowledge about all of the languages I use.
You might know about the topic you want to find. For example, you might know that you want to print something to screen, but you don’t quite know how to use print. In this case, type help(‘print‘) and press Enter. Figure 1-7 shows the results. You see complete documentation about the print keyword.
Understanding the IPY.EXE Command Line Syntax
When you open a console window, what you’re actually doing is executing IPY.EXE, which is the IronPython interpreter. You don’t have to open a console window to use IPY.EXE. In fact, you normally won’t. It’s possible to execute IronPython applications directly at the command line. The following sections discuss IPY.EXE in more detail.
Adding IPY.EXE to the Windows Environment
Before you can use IPY.EXE effectively, you need to add it to the Windows path statement. The following steps provide a brief procedure.
- Open the Advanced tab of the Computer (or My Computer) applet.
- Click Environment Variables. You’ll see an Environment Variables dialog box.
- Highlight Path in the System Variables list. Click Edit. You’ll see the Edit Environment Variable dialog box.
- Select the end of the string that appears in the Variable Value field. Type ;C:Program FilesIronPython 2.6 and click OK. Make sure you modify this path to match your IronPython configuration.
- Click OK three times to close the Edit System Variable, Environment Variables, and System Properties dialog boxes. When you open a command prompt, you’ll be able to access the IronPython executables.
Executing an Application from the Command Prompt
Normally, you execute an application by typing IPY <Python Filename> and pressing Enter. Give it a try now. Open a command prompt, type CD Program FilesIronPython 2.6Tutorial, and press Enter. You’re in the sample files supplied by IronPython. Type IPY WFDemo.py and press Enter. You’ll see a window displayed. When you click your mouse in the window, you see the word Hello displayed at each click point, as shown in Figure 1-8. If you look at the command prompt window at this point, you’ll see that the mouse cursor is blinking but you can’t type anything because the command
prompt is waiting for the IronPython interpreter to end. When you click the Close button, the application ends and you can again type something at the command prompt.
Understanding the IPY.EXE Standard Command Line Switches
Sometimes you need to provide IPY.EXE with more information about a particular application. In this case, you can use one of the command line switches shown in the following list to provide IPY.EXE with the required information. It’s important to note that the command line switches are case sensitive; –v isn’t the same as –V.
–3: Forces the interpreter to warn about Python 3 compatibility issues in your application.
–c cmd: Specifies a command you want to execute. This command line switch must appear last on the line because
anything after this command line switch is interpreted as a command you want to execute. For example, if you type ipy -c “print (‘Hello‘)“, the interpreter will output the word Hello.
–D: Enables application debugging.
–E: Ignores any environment variables that you specified as part of the Windows environment variable setup or on the command line after you started it. Some applications may not run after you use this command line switch because they won’t be able to find modules and other files they need.
–h: Displays a complete list of the command line arguments.
–i: Displays the console after running the script. You can then inspect the results of the script using console commands.
–m module: Runs library module as a script.
–O: Tells the interpreter to generate optimized code, which means you can’t perform debugging, but the application will run faster.
–OO: Removes all of the doc strings and applies –O optimizations so that the application runs even faster than using the –O command line switch alone.
–Q arg: Specifies use of one of several division options. You can use any of these values.
- –Qold (default): The precision of the output depends on the operators used. For example, if you divide two integers, you get an integer as output.
- –Qwarn: Outputs warnings about a loss of precision when performing division using integers.
- –Qwarnall: Outputs warnings about all uses of the classic division operator.
- –Qnew: The output is always a precise floating point fraction.
–s: Specifies that the interpreter shouldn’t add the user site directory to sys.path.
–S: Specifies that the interpreter shouldn’t imply that it should execute the import site command on initialization.
–t: Outputs warnings about inconsistent tab usage, which can lead to code interpretation problems.
–tt: Outputs errors for inconsistent tab usage. Inconsistent tab usage can lead to code interpretation problems, which can result in hard-to-locate bugs.
–u: Provides unbuffered stdout and stderr devices. Typically, the interpreter uses buffering to provide better application performance.
–v: Specifies that the interpreter should provide verbose output, which means that you can see everything going on in the background. You can also obtain this result by using PYTHONVERBOSE=x (where x is a True or False environment variable).
–V: Prints the version number and exits. This option is useful when you want to be sure you’re using the correct version of IronPython for your application.
–W arg: Defines the kind of warning control. Specifying these command line switches tells the interpreter to add the specified warning messages to the output. You can use any of these values:
- –Waction: Actions are one of the following strings: error (turns matching warnings into exceptions), ignore (never prints matching warnings), always (always prints matching warnings), default (prints the first occurrence of a warning for each location where the interpreter issues the warning), module (prints the first occurrence of a warning for each module where the error occurs), and once (prints only the first occurrence of a warning no matter where it appears).
- –Wmessage: Messages are Regular Expressions that define which warning messages to match.
- –Wcategory: Categories specify the class of the warning message.
- –Wmodule: Modules are Regular Expressions that define which module to match.
- –Wlineno: Line numbers are integer values that specify a line number to match. Using 0 matches all line numbers.
–x: Skips the first line of the source code, which may have special instructions that you don’t need for the current session.
Working with the –X: Command Line Switches
In addition to the standard command line switches, you also have access to the –X: command line switches, which configure the IronPython interpreter. The following list describes each of the configuration options:
–X:AutoIndent: Enables auto-indenting in the read-evaluation-print loop (REPL).
–X:ColorfulConsole: Enables ColorfulConsole support.
–X:Debug: Enables application debugging. This option is preferred over the –D command line switch because it’s newer and will enjoy a longer support period.
–X:EnableProfiler: Enables profiling support in the compiler, which helps you optimize your applications.
–X:ExceptionDetail: Enables ExceptionDetail mode, which gives you more information about every exception that occurs, making it easier to locate the source of the problem (but filling the screen much faster as well).
–X:Frames: Enables basic sys._getframe() support.
–X:FullFrames: Enables sys._getframe() with access to local objects and variables.
–X:GCStress: Specifies the garbage collector (GC) stress level. Stressing the GC can point out potential resource problems in your application.
–X:LightweightScopes: Generates optimized scopes that are easier for the GC to collect. Optimizing GC functionality tends to improve the overall performance (both speed and reliability) of your application.
–X:MaxRecursion: Determines the maximum recursion level within the application. Recursion can use a lot of system resources, so controlling the amount of recursion tends to reduce resource usage by applications that rely on recursion. Of course, reducing the recursion levels can also cause application exceptions.
–X:MTA: Runs the application in a multithreaded apartment (MTA).
–X:NoAdaptiveCompilation: Disables the adaptive compilation feature.
–X:PassExceptions: Tells the interpreter not to catch exceptions that are unhandled by script code.
–X:PrivateBinding: Enables binding to private members.
–X:Python30: Enables available Python 3.0 features, such as classic division (where dividing two integers produces an integer result).
–X:ShowClrExceptions: Displays the Common Language Specification (CLS) exception information.
–X:TabCompletion: Enables TabCompletion mode.
–X:Tracing: Enables support for tracing all methods even before the code calls sys.settrace().
Modifying the IPY.EXE Environment Variables
IPY also supports a number of environment variables. The following list describes each of these environment variables.
IRONPYTHONPATH: Specifies the path to search for modules used within an application
IRONPYTHONSTARTUP: Specifies the name and location of the startup module
Exiting the IronPython Interpreter
Eventually, you’ll want to leave the console. In order to end your session, simply type exit() and press Enter. As an alternative, you can always press Ctrl+Z and then Enter. The console will close.