Share via


Python Tools for Visual Studio

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.

The interpreters

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).

http://janatdevelopment.files.wordpress.com/2013/07/pythoninterpreters_thumb.png?w=644&h=124

A window like the one above will be opened. On this PC there are currently the versions 2.7 and 3.3 installed. If there is no Python version installed before this window will be empty. How to 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. There are 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.

http://janatdevelopment.files.wordpress.com/2013/07/newproject_thumb.png?w=644&h=397

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:

http://janatdevelopment.files.wordpress.com/2013/07/project_thumb.png?w=375&h=197

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.

http://janatdevelopment.files.wordpress.com/2013/07/pythonenvironments_thumb.png?w=370&h=235

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

01.import os
02. 
03.def greet(name):
04.  print('Hello {0}'.format(name))
05. 
06.greet('Thomas')
07.greet('Robert')
08.greet('Judith')
09. 
10.os.system('pause')

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.

Summary

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.