Debugging Using PTVS (Part 2)

I received a comment on yesterday’s post, Debugging Using PTVS, that I thought I should explore in a bit more detail than a simple answer to the comment would allow. I want to extend my thanks to Dino Viehland for making me aware of this particular issue.

The first thing you need to know is that the version of PTVS that comes with IronPython 2.7 is outdated. So, when you’re asked whether to install PTVS with IronPython 2.7, you want to clear the selection. Simply set the Tools for Visual Studio option shown below not to be available.

IP2702

After you install IronPython 2.7, download and install PTVS from the CodePlex site instead. If you already have IronPython 2.7 installed with PTVS, I’ve found that the only way to truly get rid of the PTVS support is to uninstall it and start from scratch.

OK, on to the debugging part of this post. It turns out that you can change the debugging mode of PTVS by modifying the project properties. To perform this task, you choose Project > ProjectName Properties. If your Project Properties window looks like the one shown here, then you truly do have the old PTVS installed.

PTVSDebug201

However, if you see this version:

PTVSDebug202

you’re ready to go. Notice that the Debug tab has a Launch Mode field. This field controls how PTVS debugs your application. My initial post, Debugging Using PTVS, shows the IronPython (.NET) Launcher side of things. It turns out that you can change this setting to Standard Python Launcher, which significantly changes the behavior of the debugger.

The down side firstyou lose the ability to debug other .NET code. All you see when using this launcher is the Python world.

Now for the good part. My previous post showed how the call stack looks. It’s barely readable, but only if you squint a bit and think about it for a second or two. When working with the Standard Python Launcher, the call stack is significantly easier to read. Check it out:

PTVSDebug203

In addition, you’ll find that the Immediate window is a bit more cooperative too. Remember that it wasn’t possible to type ? args[0] without generating an error in yesterday’s post. When working with the Standard Python Launcher you get the expected result as shown here:

PTVSDebug204

In fact, you’ll find that debugging Python-specific code becomes easier when using the Standard Python Launcher. So, using the newer version of PTVS gives you some significant debugging flexibility that developers really need to create great IronPython applications. Let me know your thoughts on the updating debugging functionality at John@JohnMuellerBooks.com.

 

Debugging Using PTVS

A reader recently wrote to ask about the debugging features provided by PTVS. In Chapter 12 of Professional IronPython you discover techniques for debugging your IronPython application. This chapter doesn’t include any information about PTVS because the product wasn’t available at the time of writing. However, the techniques described in the chapter work just fine in Visual Studio too. What PTVS does provide is some additional debugging functionality that isn’t available to IronPython developers normally and that’s the topic of this post.

This post begins with the example created in the Using the PTVS for a Windows Forms Project post so that you can easily follow along. Of course, the techniques I describe are useful for any application you might be working on at the moment.

One of the first things you should look at is the Output window. It shows you everything that IronPython is loading in order to run your application. This information can help you locate potential issues in running an application on a client system and help you create the appropriate installation support for your application as shown here.

PTVSDebug01

As with any other Visual Studio application, you can set breakpoints for your IronPython application to stop execution at a particular point. To set a breakpoint you can click in the left margin. It’s also possible to right click a particular line, select the Breakpoint menu entry, and choose from the options on the menu. PTVS even provides full support for the usual assortment of conditional breakpoint offerings as shown here. (For the purposes of this post, I set a breakpoint on the MessageBox.Show(‘Hello!’) line of code.

PTVSDebug02

When your application stops at a breakpoint, you have access to many, but not all of the Visual Studio features. So far I haven’t been able to get the Autos window to work. (If anyone has a trick to make it work, let me know.) The Locals window does work though and provides the full range of information you’d expect. Here’s an example of the information you’ll typically see.

PTVSDebug03

Notice that you can drill down into the arguments as needed. It’s also possible to change values using the Locals window, just as you can with Visual Studio normally. Changes to many properties and variables work fine. However, you might not be able to count on this behavior for all value types. Visualizer support is there as well. At least, the Text, XML, and HTML visualizers seem to work. There may not be any support for custom visualizers.

The Call Stack window works much like it does for any Visual Studio application. Of course, the information is IronPython-specific as shown here.

PTVSDebug04

One bothersome aspect is that the window doesn’t tell you the correct language. Instead, you see that the language is marked as Unknown. The debugger does get the line numbers correct and it’s easy enough to decipher the remaining information in this window. You’ll find that the Call Stack window correctly marks any arguments to methods for you.

The Immediate window only partially works. At least, I found it worked only partially for me. For example, if you type ? args and press Enter, you’ll find that the Immediate window correctly tells you that it’s a tuple containing two items. However, it appears to be impossible to access the elements within tuple. For example, if you type ? args[0] and press Enter, you’ll find that the Immediate window responds with an error message as shown here.

PTVSDebug05

Making things a bit more difficult is the fact that you can type ? $function and press Enter to obtain the function-specific information and type ? $function.__name__ and press Enter to obtain the function name. In this case, everything works as expected. The debugging support provided by the Immediate window seems spotty. You may find yourself limited to performing certain tasks using it and then relying on the Locals window to perform other tasks.

There are other windows supported by the debugger that appear to work as you think they should. For example, you can set watches and view them using the Watch window, just like any other language that Visual Studio supports. The Breakpoints window also seems to work as it should.

This has been a whirlwind overview of the debugging features provided by PTVS to IronPython developers. Let me know if you have any specific questions at John@JohnMuellerBooks.com and I’ll do my best to research them.

 

Using the PTVS for a WPF Project

As with Silverlight, there is no mention of theWindows Presentation Foundation (WPF) in my book, “Professional IronPython.” The WPF is supposed to help solve some display problems for application developers and allow better use of modern graphics processors. Microsoft has provided a significant amount of information about the WPF that I won’t be discussing in this post. Whether you find WPF helpful or not depends on the kind of applications you create. I’m sure that WPF will find use in game applications, widgets of various sorts, mini-applications, and a host of other uses, but I’m not sure just what it adds to the common business application, such as a data entry application. Still, WPF is an exciting new technology that you should investigate.

As with the Silverlight project in my previous post, you need to have the PTVS installed on your system to work through this example. To begin this example,
you start Visual Studio as normal and choose File | New to create a new
project. The New Project dialog box should contain an entry for a Wpf Application template as shown here. If you don’t see
this template, then you probably don’t have IronPython 2.7 installed on
your system and can use the instructions found in the IronPython 2.7 and PTVS blog entry to install it.

WPF01

The example will accept the default name for the project. Of course, you can use any name that you’d like. Click OK. You’ll see a new WPF application. However, for some odd reason, the eXtensible Application Markup Language (XAML) (pronounced zammel) file doesn’t always open, so you’ll have to open it manually. At this point, you’ll see a project similar to the one shown here.

WPF02

The top half of the designer shows what the page will look like, while the bottom half shows the associated XML. As you add controls to the form and configure them, you’ll see the changes in the XML, making it easy to code changes to the controls when necessary. Of course, you can choose to display just the Design window or just the XAML window if desired. Depending on the amount of screen real estate at your disposal, collapsing one of the panes by clicking Collapse Pane at the right side of the window, may be a good idea.

One of the advantages of using WPF is that you have immediate access to the Ribbon without having to employ any odd programming techniques. You just drag and drop the Ribbon controls from the Toolbox as you would any other control. Here’s a subset of the default set of WPF controls you can access from IronPython.

WPF03
As you can see, the set of controls is rich and you do have access to others, so WPF may be the way to go for IronPython developers who need maximum control access.

The example isn’t going to do anything fancy. I’ve added two Button controls to the form. Here is how I set the various properties using XAML:

<Window
   Title="WpfApplication1" Height="300" Width="300">
   <Grid>
      <Button Content="Click Me"
              Height="23"
              HorizontalAlignment="Left"
              Margin="191,12,0,0"
              VerticalAlignment="Top"
              Width="75"
              Uid="btnDisplay"
              Click="btnMessage_Click" />
      <Button Content="Cancel"
              Height="23"
              HorizontalAlignment="Left"
              Margin="191,41,0,0"
              VerticalAlignment="Top"
              Width="75"
              Uid="btnCancel"
              Click="btnCancel_Click" />
   </Grid>
</Window>

The first button displays a simple message box, while the second ends the application. Each Button control is assigned basic visual properties, a name, and an event handler for its Click() event. One thing I dislike about working with WPF is that the Properties window doesn’t display any quick help for the various properties. You need to know what each property does or look it up, which is really inconvenient.

The code behind for this example relatively simple. As previously mentioned, the first control displays a message, while the second closes the application as shown here:

import wpf
 
from System.Windows import Application, Window, MessageBox
 
class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication1.xaml')
     
    def btnCancel_Click(self, sender, e):
         
        # End the application.
        MyWindow.Close(self)
     
    def btnMessage_Click(self, sender, e):
         
        # Display the message on screen.
        MessageBox.Show("Hello")
     
if __name__ == '__main__':
    Application().Run(MyWindow())

Much of this code is automatically generated for you. In order to use the MessageBox class, you must add it to the import statement as shown in the code. The code begins with the initialization, which simply loads the components from WpfApplication1.xaml.

The btnCancel_Click() event handler relies on the MyWindow object’s Close() method. Make sure you pass a copy of self as part of the call.

The btnMessage_Click() event handler makes a call to the MessageBox.Show() method with a simple text message. You’ll see the dialog box appear as expected.

From what I can see, the WPF support for IronPython in PTVS is on par with WPF support for any other .NET language. It has the same limitations and benefits as any WPF application will have. So, what do you think of the new WPF project support provided by PTVS? Let me know at John@JohnMuellerBooks.com.

 

Using the PTVS for a Silverlight Project

My book, “Professional IronPython” doesn’t discuss how to work with technologies such as Silverlight, so I thought I’d see how hard it would be given that PTVS comes with support for a Silverlight project. This post adds to my previous post about PTVS. Of course, you’ll need to have PTVS installed on your system before you begin working with this example.

To begin this example, you start Visual Studio as normal and choose File | New to create a new project. The New Project dialog box should contain an entry for an IronPython Silverlight Web Page template as shown here. If you don’t see this template, then you probably don’t have IronPython 2.7 installed on your system and can use the instructions found in the IronPython 2.7 and PTVS blog entry to install it.

Silverlight01

Give your application a name (the example uses the default of SilverlightPage1) and click OK. You’ll see a new project appear like the one shown here:

Silverlight02

What I find interesting is that there is a designer provided for this template, but none provided for the Windows Forms application. I tried the Designer out and it works well. There isn’t any difference in creating a Silverlight application using this template than any other language I’ve tried. However, the list of controls you can access is limited. Here’s what the default Toolbox entries look like:

Silverlight03

I tried adding a control to the list, just to see what would happen. When you open the Choose Toolbox Items dialog box, you’ll see that all of the normal controls are already selectedthey simply don’t appear in the Toolbox. I even tried checking a few controls on the Silverlight-specific tab without any result. So, my thought is that you’re limited to the controls show. If you know otherwise, please contact me at John@JohnMuellerBooks.com. Even so, you can create a business application without any problem using the controls provided.

The Silverlight front end for your project uses HTML-like statements, as you might expect. You create linkage between the Web page and your IronPython code behind just as you would any other language. Here’s what the default sample code looks like:

<script type="text/python" src="SilverlightPage1.py"></script>

The code behind looks just like any other IronPython code you might have created in the past. Here’s the default example code:

def SayHello(s,e):
    window.Alert("Hello, World!")
  
document.Button1.events.onclick += SayHello

In short, you don’t have to worry about any odd programming techniques to make this example work. I was frankly surprised at just how nicely things do work with this template. There are a few oddities you’ll encounter. The first is that when you press F5 to start debugging your application, the IDE starts the Chiron development server, rather than the default. That’s not too odd, but it’s something you should know about.

The second is that the application doesn’t automatically appear in your browser. So, you’re waiting for something to happen and nothing does. Instead, you must right click the server entry in the Notification Area and choose Open in Web Browser from the context menu. At this point, I received a directory listing like the one shown here, rather than the application I expected.

Silverlight04

Clicking SilverlightPage1.html did display the application. In fact, it displayed a lot more. I also received an IronPython Console I could use for debugging tasks, as shown here:

Silverlight05

As shown in the figure, you can minimize the IronPython Console when not needed, so you can see your application better. Overall, I was very impressed with the Silverlight template offering and think it’s a great addition to the IronPython developer’s toolbox. Be sure to look for additional PTVS entries in my blog as time permits and to let me know your thoughts about it!

Using the PTVS for a Windows Forms Project

Chapter 8 of Professional IronPython demonstrates how to create a Windows Forms application using IronPython 2.6. As mentioned in my previous post about IronPython, IronPython 2.7 provides something new in the way of integration, PTVS. You’ve already discovered the means to create a console application using PTVS in my second post on this topic. This post goes a step further and demonstrates how to create a Windows Forms application using the same approach you’d normally use with any other .NET language, such as Visual Basic .NET or C#. I assume you already have PTVS installed on your system.

Of course, you begin by opening Visual Studio 2010 as normal. The New Project dialog box should contain a WinForms Application template in the IronPython folder. If you don’t see the correct template, then you probably don’t have IronPython 2.7 installed with Visual Studio 2010 (see my previous post for details). Call this project PythonApplication2 as shown here:

WinForm01

Click OK and Visual Studio creates the solution for you. The next step is to recreate the display shown in Listing 8-1 of the book. I had truly hoped that PTVS would provide Designer support, but it doesn’t. Here’s what you get as output from the template:

WinForm02

Now, it is disappointing that you don’t get a Designer, but the template does provide you with some helpful information and it’s better than what you started with in Chapter 8. The updated code is easier to read and requires that you perform less typing than shown in Figure 8-1. Here’s the new version of Listing 8-1.

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
 
from System.Windows.Forms import *
from System.Drawing import *
 
class MyForm(Form):
    def __init__(self):
         
        # Add a tooltip control.
        Tips = ToolTip()
 
        # Configure btnOK
        btnOK = Button()
        btnOK.Text = "&OK"
        btnOK.Location = Point(263, 13)
        Tips.SetToolTip(btnOK, 'Displays an interesting message.')
 
        # Configure btnCancel
        btnCancel = Button()
        btnCancel.Text = "&Cancel"
        btnCancel.Location = Point(263, 43)
        Tips.SetToolTip(btnCancel, 'Cancels the application.')
 
        # Configure lblMessage
        lblMessage = Label()
        lblMessage.Text = 'This is a sample label.'
        lblMessage.Location = Point(13, 13)
        lblMessage.Size = Size(120, 13)
 
        # Configure the form.
        self.ClientSize = Size(350, 200)
        self.Text = 'Simple Python Windows Forms Example'
        self.AcceptButton = btnOK
        self.CancelButton = btnCancel
 
        # Add the controls to the form.
        self.Controls.Add(btnOK)
        self.Controls.Add(btnCancel)
        self.Controls.Add(lblMessage)
 
form = MyForm()
Application.Run(form)

This code works precisely the same as the code described in Listing 8-1. You still see the same dialog box as output as shown here:

WinForm03

It isn’t anything fancy, but the example does demonstrate that you can create a Windows Forms application using straightforward code and less typing than the example in Listing 8-1. Of course, this form requires event handlers, just like the ones used for the example in Chapter 8. Of course, you begin by creating the required linkage after you initialize the form as shown here:

# Add the event handler linkage.
btnOK.Click += btnOK_Click
btnCancel.Click += btnCancel_Click

After which, you add the event handler code shown here:

# Define the event handlers.
def btnOK_Click(*args):
     
    # Display a message showing we arrived.
    MessageBox.Show('Hello!')
 
def btnCancel_Click(*args):
     
    # Close the application.
    form.Close()

At this point, you have the same first example shown in Chapter 8. Of course, Chapter 8 contains a number of other examples that demonstrate other techniques; all of which you can easily move to the new environment provided by PTVS. Here’s the source code for the entire example: Program.py. So, what do you think of the new Windows Forms project support provided by PTVS? Let me know at John@JohnMuellerBooks.com.

IronPython 2.7 and PTVS

The number of new features for Professional IronPython keep increasing as I discover more about IronPython updates from the community. You may have read my PTVS post the other day and wondered just how it was that the PTVS installer didn’t detect my IronPython 2.6.2 setup. It turns out that the PTVS installation currently has some problems. The best course of action is to uninstall PTVS and your IronPython 2.6.2 installation as well. (Make sure you reboot your system after you uninstall the old products.) What you really need is to download and install IronPython 2.7 Release Candidate 2.

Start the installation after you complete the download. You’ll see the normal licensing agreement and so on. However, the important feature to observe are the options on the Custom Setup page shown here:

IP2702

Notice the Tools for Visual Studio entry. Installing this feature ensures that you get all four of the templates in Visual Studio 2010. Once you complete the installation, you can check it out in Visual Studio. Here’s the updated New Project dialog box from my copy of Visual Studio 2010 Ultimate:

IP2701

So, now I have a number of new toys to play with. Future blog entries will describe what I find when I try the other templates out. In the meantime, I’d love to hear about your experiences with PTVS at John@JohnMuellerBooks.com.

 

Using the PTVS for a Simple Project

Well, I installed the PTVS I mentioned in yesterday’s post on my system and gave it a whirl. I must admit to being a little underwhelmed after the hype on the CodePlex site. However, the download does help and if you want to use Visual Studio for your development platform you should get it.  The process for creating a project is certainly easier than then one described in Chapter 2 of my book (although, that process still works fine for existing projects). The first thing you’ll notice after installation is that you get a new installed template as shown here.

PTVS01

As you can see, you do get all of the functionality that you’d normally get in a Visual Studio project, including the ability to add your application to source control, so this is a good start. After you configure your solution, the IDE creates it for you and you see a single file with some test code in it like this.

PTVS02

Don’t get the idea that you can simply click Start Debugging at this point and see something interesting. Before you can do anything, you have to configure the interpreter. Choose Tools > Options to display the Options dialog box. In the Options dialog box, select the Python\Interpreter Options folder. Here’s what the options look like; I’ve already configured mine for use on a 64-bit Windows 7 system.

PTVS03

I found that IntelliSense worked great. For example, when I typed raw_input(, I automatically received the proper help as shown here.

PTVS04

I played around with the IDE quite a lot more and was impressed with what the IDE does now that it didn’t do in the past. Of course, I’m going to have to play a lot more before I feel comfortable with everything this add-in can do.

So, where was the disappointment factor? Well, the first issue is that I was supposed to receive a total of four template types with IronPython according to the ReadMe.html file that comes with the product. I’m hoping there is a simple fix for this issue because I’d really like to tell you about the other templates that PTVS supports. The second issue is that the IDE didn’t automatically recognize my interpreter as it should. I’m assuming this is the reason why I didn’t receive the additional template. I’ll report back about these issues and show you more about PTVS as time permits. In the meantime, let me know your thoughts about PTVS at John@JohnMuellerBooks.com.

 

IronPython Finally Has Visual Studio Support

Months ago when I wrote Professional IronPython, I had to show you all kinds of workarounds for seemingly simple problems because the Visual Studio IDE didn’t provide the support required to do things like create an IronPython project.  For example, in Chapter 8 I have to show you how to create a Windows Forms application without using the visual designer.  That’s right, you need to write all of the component code manually, rather than rely on the GUI.  Microsoft’s decision not to support IronPython and IronRuby any longer seemed to put a nail in a great product’s coffin and I thought that perhaps the days of IronPython were numbered.

Fortunately, I was wrong. Microsoft has finally decided to release a beta add-in for Visual Studio 2010 that provides IDE support for both CPython and IronPython called Python Tools for Visual Studio (PTVS). You’re not getting any sort of new functionality from the language perspective.  The add-on relies on the underlying language features to perform its work.  The benefit to using this add-in is that it allows you to use the IDE to perform tasks such as creating an application using a template, rather than coding everything by hand.

Of course, the add-in provides far more functionality than simply creating projects.  The fact that I now get IntelliSense support is amazing.  You don’t know how helpful an IDE feature is until you try to write code without it.  Over the years, I’ve become somewhat addicted to IntelliSense because it helps me “remember” what it is that I want to do next.  Otherwise, I have to sit there and think about how things are supposed to go together; not always an easy task when you regularly work with multiple languages.

The add-in must be striking a chord with everyone.  It was only released on the 7th and there have already been 1,380 downloads (as of yesterday when I downloaded my copy).  If you program with IronPython and you often use IronPython to overcome procedural language limitations, this is a must have add-in for Visual Studio.

I’ll be working with this add-in over the coming weeks and will report back on what I find.  In the meantime, I’d love to hear your input on it at John@JohnMuellerBooks.com.