Using Visual Studio to Create IronPython Applications

0
242

You might have looked at the New Project dialog box after you installed IronPython, assuming that you’d find a series of new project templates. Unfortunately, you won’t find any new templates for IronPython. The current version of the product doesn’t include anything you can use directly. Fortunately, you can still create a project for IronPython projects and use Visual Studio to edit and debug it. The following sections take you through a simple configuration scenario and then show the resulting project in action.

Creating the Project

Before you do anything else, you must create a project to hold your IronPython application. The following steps show you how to perform this task.

  1. Open Visual Studio, but don’t open any project or template files.
  2. Choose File ➪ Open ➪ Project/Solution. You’ll see the Open Project dialog box shown in Figure 2-1.

    Use the Open Project dialog box to start the project.

  3. Locate and highlight IPY.EXE (normally found in the Program FilesIronPython 2.6 folder). Click Open. Visual Studio creates a solution based on IPY.EXE, as shown in Figure 2-2. You must still configure this solution.
    IPY.EXE becomes the focal point for a new solution.
  4. Right-click IPY in Solution Explorer and choose Properties from the context menu. You’ll see the General tab of the Properties page shown in Figure 2-3. (Your display may differ slightly from the one shown in Figure 2-3 based on your machine configuration and the Visual Studio 2010 edition you use.) At a minimum, you must change the Arguments and Working Directory fields to match your project.
    Modify the properties to match your project requirements.
  5. Select the Arguments field. Type -D NameOfProject, where NameOfProject is a Python (.py) file. For example, the example project uses MyFirst.py, so you’d type -D MyFirst.py. Remember that the -D command line switch turns on debugging. You can find other command line arguments listed in the “Understanding the IPY.EXE Command Line Syntax” section. Include any other command line switches you want to use in the Arguments field.
  6. Select the Working Directory field. Visual Studio will default to using the Program FilesIronPython 2.6 directory — a directory that you’re unlikely to use to hold your source code files. Change the Working Directory to match your source code directory. Click the ellipses to locate the directory on your hard drive using the Browse for Folder dialog box.
  7. Choose File ➪ Save All. You’ll see the Save File As dialog box shown in Figure 2-4.
    Save the resulting solution before you do anything else.
  8. Locate the folder you want to use to save the project in the Save In field.
  9. Type a name for the solution in the Object Name field. Click Save. Visual Studio will save the project to the folder you selected.

Adding Existing Files to the Project

At this point, you have a project without any files in it. Yes, you could run the project and you’d see what you’d expect, but you can’t debug the IronPython file or edit it. The following steps tell how to add a file to your project.

  1. Right-click the solution entry in Solution Explorer (not the IPY project entry) and choose Add ➪ Exiting Item from the context menu. You’ll see the Add Exiting Item dialog box shown in Figure 2-5.
    Add your existing Python files to the project.
  2. Locate the existing file you want to use and click Open. Visual Studio adds a Solution Items folder to Solution Explorer and places the file you selected in the Solution Items folder, as shown in Figure 2-6. In addition, Visual Studio automatically opens the file for you.
    The Solution Items folder holds the Python files you add.

Adding New Files to the Project

Once you get used to working with Visual Studio, you may decide to create files from scratch using the Visual Studio IDE. In this case, you need to add blank (new) files to the project. The following steps show you how to perform this task.

  1. Right-click the solution entry in Solution Explorer and choose Add ➪ New Item from the context menu. You’ll see the Add New Item dialog box shown in Figure 2-7.
    You can use the Visual Studio IDE to create new Python files.
  2. Highlight the Text File entry. Visual Studio will assume you want to create a text (.TXT) file, but you can change the extension to anything you want.
  3. Type the name of the Python file you want to create in the Name field. Make certain that your file has a .py extension or the IronPython interpreter may not work with it.
  4. Click Add. Visual Studio adds the file to Solution Explorer (similar to the addition shown in Figure 2-6) and automatically opens the file for editing.

IronPython Project Limitations

The project you create using this technique has some serious limitations. Here’s a partial list of the things that you won’t see in your IronPython project that you’ll normally see in other Visual Studio projects.

  • Color support for keywords or other special text
  • IntelliSense
  • New Items dialog support
  • Immediate window (debugging)
  • Command window (when working with variables during debugging)

Debugging the Project

This section assumes you’re using the MyFirst.py example found in Chapter 1 and that you’ve created a project for it. Start by placing a breakpoint on the first line of the application (print(‘5 * 10 =‘),); then place a second breakpoint at the beginning of the function (def mult(a, b):). You can do this by placing your cursor on the line and pressing F9 or choosing Debug ➪ Toggle Breakpoint. You should see the breakpoint shown in Figure 2-8.

Visual Studio helps you debug your IronPython applications.

At this point, you can begin debugging your application. The following steps get you started.

  1. Press F5 or click Start Debugging to begin debugging your application. Starting the debug process can take a while because Visual Studio has to start a copy of the IronPython interpreter. Visual Studio stops at the function definition. IronPython makes a list of function definitions when it starts the application.
  2. Click Step Over. You’ll move to the first line of the application. At this point, the debugger begins executing your application code.
  3. Click Step Over again. If you look at the command prompt at this point, you’ll see that it contains the expected output text, but not the answer, as shown in Figure 2-9. Now, if you clicked Step Over again, you’d see the output from the Mult() function, but you wouldn’t actually see the code in Mult() execute. The next step shows how to get inside a function so you can see how it works.
    The console screen will show the results of tasks performed in your application code.
  4. Press F5 or click Start Debugging. The application will stop within Mult(). Being able to stop within a function is the reason for setting the second breakpoint at the beginning of this procedure. Now you can use Step Over to execute the lines of code one at a time. Notice the Debug History window. You can select entries in this window to see what the IronPython interpreter has been doing in the background, as shown in Figure 2-10.
    Use the Debug History window to see what the interpreter is doing in the background.
  5. Press F5 or click Start Debugging. The application will end.

Visual Studio does provide you with access to many standard debugging features. For example, you can place variables in the Watch windows and see their values as shown in Figure 2-11. You also have access to the Call Stack and Output windows. The Immediate and Command windows don’t work as you might expect them to, so you need to inspect variables and perform other variable-related tasks using the Watch windows.

The Watch windows provide access to variable information.