In the first part of this series we looked at installing the latest version of the Python Tools for Visual Studio (PTVS) using the free Visual Studio Integrated Shell along with the free Enthought Python Distribution. In this installment we’ll look at using PTVS and the Pyvot add-in to create and manipulate Excel data as well as an example of using NumPy and SciPy to generate a histogram.
NumPy and SciPy have a large following in the academic community and are used by researchers and students alike to perform serious scientific computations. There were a large number of tutorials and papers covering different aspects of these packages at the recent Pycon 2012 conference held in Santa Clara, Ca. Now with PTVS you can use the power of the Visual Studio IDE to build and test Python applications utilizing SciPy and NumPy on the Windows platform
Pyvot and Excel
Pyvot is distributed as an .msi file, which must be installed separately from PTVS. It also requires CPython 2.6 or 2.7 along with the Python for Windows extensions. You’ll get both of those if you install the EPD free distribution as described in part 1. You’ll find the Pyvot download on the PTVS codeplex site. There are a few additional steps that must be completed once the basic installation completes. This consists of actually installing Pyvot into the locally installed Python instance. You can accomplish this right from the Visual Studio IDE (see Figure 1).
Figure 1: Install Pyvot into Python 2.7
Once you have Pyvot installed there will be a new template application available from the Visual Studio new project menu (see Figure 2). This creates a shell application (see Figure 3) that will create a fixed set of rows and columns in an instance of Excel (see Figure 4). Note that you’ll need a copy of Excel installed to actually run the sample. Pyvot provides a two-way bridge to Excel, meaning you can both “set” values in an Excel spreadsheet and “get” or read from an open Excel spreadsheet. All communication is handled using COM and is done through the Excel application object model.
Figure 2: New template application
Figure 3: Shell application
Figure 4: Fixed set of rows and columns in an instance of Excel
All the functionality from the Pyvot package is also available from the Python interactive command line. It’s fully dynamic as well, which means you can make changes in Excel and then see the results from the Python command line. The interactive interpreter is perfect for testing a chunk of code without the need for the typical compile-then-run loop. You’ll find a link to a YouTube video on the PTVS site showing how to interact with Excel from both the Python interactive command line and from code.
Scientific Computing with NumPy and SciPy
One of the best ways to quickly get up to speed on a new library or coding technique is to look at sample code. The SciPy.org site includes a Cookbook page with a long list of sample programs for various scientific computation scenarios. The EPD free distribution also includes Matplotlib for plotting purposes. We were able to copy a sample program from the Matplotlib site (see Figure 5) and run it without modification to produce the graph shown in Figure 6.
Figure 5: Sample program from the Matplotlib
Figure 6: Sample program graph
The interactive command window works well when experimenting with different plot types. You’ll also have access to code completion and IntelliSense from the interactive window as well (see Figure 7). Python uses the triple quote convention to provide a standard way of documenting modules. This documentation is available through the reserved help command and also shows up when you type in the name of a function to give you a hint on what parameters are required (see Figure 8).
Figure 7: Code completion and IntelliSense
Figure 8: Hints
In this two part series we’ve shown you how capable Visual Studio is for real-world Python code development. The Pyvot add-in delivers a wealth of possibilities for writing code to manipulate Excel data. With the addition of NumPy and SciPy you have a full-featured scientific computation and visualization capability for free!