A few weeks ago an interesting project was shared with me through the Windows Azure Insiders. Microsoft wanted some feedback on their upcoming extension for Visual Studio – Python Tools for Visual Studio. As a fan of Python, and as someone who hadn’t found a good IDE for Python yet, I asked for an early access to it.
This week Scott Hanselman announced the public availability of it. Today I want to take the chance to give you a quick overview about the possibilities the Python Tools gives us as developers.
Install the tools
The tools are available on the official CodePlex for the Python Tools for Visual Studio – http://pytools.codeplex.com/
Just go to the Downloads section. Here you will have several options: Python Tools for Visual Studio 2012, Python Tools for Visual Studio 2010, and some samples. Download the msi installer which fits your needs best. If you want to have a look at some sample implementations, which are also available in the documentation, you can download them on the same site.
Python currently has two popular versions: 2.7 and 3.x. The recommended one for the Python Tools is version 2.7. To find out which interpreters are installed on your machine open Visual Studio and open the Python Interpreters windows (Tools – Python Tools – Python Interpreters).
A window like the one above will be opened. On my PC there are currently the versions 2.7 and 3.3 installed. If you don’t have installed Python before this window will be empty. How can you get a Python version? Option #1 would be to click the link Go online and help me find another interpreter. The second option is to browse to the official Python page and open the download section. Here you will find most of the current Python interpreters.
Create a simple Python project
Now that we have setup the environment let us create a small Python application. To create a Python application click File – Project – New Project. In the New Project screen you will see all the common project types – and Python.
From here you can create several project types:
- Create an application from existing Python code
- A normal Python application
- A Django application
- An IronPython application
- An IronPython Windows Presentation Foundation application
- An IronPython Silverlight website
- An IronPython Windows Forms application
To keep things simple I choose the normal Python application project type. This creates the following skeleton:
Most interesting for us at this point are the Python Environments and the PythonApplication2.py file. Let’s start with the Python environments. Currently there’s only the default Python interpreter associated with our project (the one which is bolded in the Python interpreters window). To add other interpreters right-click Python Environments and select Add/Remove Python Interpreters…
The opened window gives you the chance to select the Python environment to use with this project. In this sample I would like to use both of my installed interpreters, Python 2.7 and Python 3.3.
If you click the small arrow beside your environment you will see the packages installed along with this interpreter. If you want to install other packages right-click the appropriate interpreter and choose Install Python Package…
One thing to notice at this point: If you install packages this way they will also be available in other projects. What’s the problem? Imagine the following situation: You’re working on two different projects and you need to work with two different versions of the Azure tools. To prevent conflicts in the packages you can create a virtual environment, which is like a clone of an interpreter, but the packages are limited to the project where the virtual environment lives in. To create a virtual environment right-click Python Environments and choose Add Virtual Environment…
The following dialog gives you the options to give it a name and select the interpreter the virtual environment will created from.
At this point we have done several things:
- We have installed the Python Tools
- We installed another interpreter
- We created a Python application
- We created a virtual environment
What’s missing? Some simple code!
Before we start we have to make sure that our virtual environment is our current one. Right-click the virtual environment and select Activate Interpreter.
Alright, let’s create a simple greeter. The Python code looks like this
This will greet Thomas, Robert, and Judith for us. I recommend to type the code yourself to see how the Visual Studio Intellisense helps you writing.
The Python Tools for Visual Studio makes it easy to work with Python inside of Visual Studio. In this blog post we have installed the tools, installed an interpreter, created a simple Python greeter, and added a virtual environment to our project.
- Jan (@Horizon_Net)