How to Develop Python in Visual Studio... and Mix it Up with the .NET Framework!

PTVS Banner

I’ve been learning about the Raspberry Pi for a few months, occasionally writing about it. I usually start on a laptop using JetBrains’ PyCharm IDE, which is much faster than the Pi, then move everything over when it’s done and I need to run it against the GPIO pins.

But Visual Studio was my main coding environment for years, and I got really comfortable with it (well, when they weren’t moving my cheese), so when I realized it could support Python I had to check it out. If you’re doing dev in Python, and are familiar with Visual Studio, read on…


What are the Python Tools for Visual Studio?

The Python Tools for Visual Studio (PTVS) plugin brings Python support to Visual Studio.

When Microsoft released Update 3 for VS 2015 a few weeks ago, I noticed an option for “Python Tools for Visual Studio”. I thought it might be a new feature, but it turns out the PTVS plugin has been around for at least 5 years, first on CodePlex and now on GitHub(adding it directly to the “update” dialog, however, may be new…)

Python Tools for Visual Studio (PTVS) is an open-source plug-in for Visual Studio which supports programming with the Python language. It lets you bring your own Python interpreter, including CPython, IronPython, PyPy, and more, and supports a broad range of features from editing with IntelliSense to interactive debugging, profiling, interactive REPLs with support for IPython, cross-platform and cross-language debugging support, and deployment to Microsoft Azure.

Installing the PTVS Plugin

According to the release notes for 2.2.4, you need VS2015 for the latest version. Or you can install older versions of PTVS, if all you’ve got is an older version of VS, but VS2015 Community is free so you might as well grab a copy.

PTVS 2.2.4 and later will not support Visual Studio 2013 or earlier. If you are unable to obtain any of the editions of Visual Studio 2015, the last release of PTVS for Visual Studio 2013 was PTVS 2.2.2 and for Visual Studio 2010 and 2012 was PTVS 2.1.1.

There are a few ways to get the plugin installed.

Option 1: Select During a Major VS Update

If you’re currently installing an update to VS anyway, just select the option for PTVS.

VS2015.3 Python Tools 1

Option 2: Modify VS in Add/Remove Programs

You can bring up the same “update” dialog any time you want, by finding VS in the Add/Remove Programs panel and double-clicking it, then choosing “Modify” in the VS window.

modify visual studio from windows add remove programs dialog

Option 3: Download from GitHub

You can go directly to the latest release on GitHub, and download an executable from there.

You might want to check that page out anyway. They’ve got a sample pack you can download (more on that later).

Option 4: Install from Within VS

One more way. Open the “New Project” dialog inside Visual Studio and try to create a new Python project. Oops, no projects available… if the plugin isn’t installed, you’ll be prompted to do it now.

create new python project in vs2015

Whatever way you choose, once it successfully completes, you should have a lot more options available the next time you try to create a new Python project.

available Python projects in VS2015

Install a Python Interpreter

So now Visual Studio supports Python. (woot!) But you still need to install Python… or more accurately, an *interpreter *that knows what to do with the code you’re  writing.

There are quite a few interpreters, and they each have their pros and cons. For example, the CPython interpreter provides maximum compatibility with the official language specs, while the PyPy interpreter executes your code faster, and the IronPython interpreter integrates your code with the .NET Framework. Microsoft describes a few (there’s other helpful info on that page too), and I found The Hitchhiker’s Guide to Python: Picking an Interpreter to be a nice, straight-forward guide as well.

If you try to run some Python code without at least one interpreter installed, VS will tell you exactly where to go. To find an interpreter, that is.

vs couldnt find any interpreters

CPython (the default interpreter)

CPython is the “official” interpreter, and will support every piece of Python code you throw at it. Unless you’ve got a specific reason to do otherwise, embrace the future and download the Python 3 version.

Note: One of the more annoying decisions to have to make is between Python 2 or 3. Usually, when a language spec is updated, it maintains backward-compatibility. With Python 3, they opted to dump some of the baggage from Python 2, and that means breaking changes. Python 3 is the way of the future, but many old and useful packages are still written in Python 2 and won’t just “work” with Python 3.

IronPython (the .NET Compatibility interpreter)

Since we can install multiple interpreters, I installed IronPython too, so I could experiment with its .NET capabilities. It targets Python 2, but has at least partial support for 3, because both print 'hi' and print('hi') work, even though the former is Python 2 syntax and the latter is Python 3.

Although you can download IronPython separately, it’s also offered as a NuGet package, which makes it really easy for you to share your code with someone. Their VS will download IronPython (and any other dependencies you’ve specified) via NuGet.

IronPython is also available as a NuGet package. The standard library is a separate package. This is the recommended way to get IronPython if you are embedding it in another program.

Unfortunately for me, everything I tried with NuGet failed. It was disabled in my Python project, and when I tried to install from the console, it complained very loudly. Something to figure out later I guess.

nuget yelling at me vs manage nuget packages opt grayed out environment unknown python 27 appears to be incorrectly configured or missing

Running a “Hello World!” Script

Installing the interpreter separately as described above worked just fine, and I was able to run my first “hello world” scripts from VS.

… from within a project

Create a new Python Application and try running something simple.

hello world from python app in vs

If you installed IronPython too, close the previous app and create an IronPython Application now. Try something simple again. Notice that creating an IronPython App loads the IronPython interpreter for us (in Solution Explorer) instead of CPython. Convenient!

hello world from ironpython app in vs

… from within the REPL

There are REPL (read-eval-print-loop) windows for each interpreter too, for when you want to test some Python code without the overhead of creating a project.

Select “View » Other Windows” from the menu (or “Tools » Python Tools”) and look for items ending in “Interactive”.

repl for ironpython

You can even open up multiple REPL windows, and try out your Python 2.x and 3.x code side-by-side.

repl for ironpython 2x and python 3x

PTVS Samples

I briefly mentioned the samples pack at the beginning. If you’d like to check them out, go to the release page on GitHub and download the VSIX files, then double-click each one to install them.

Restart VS and you should see some new projects available under Python.

new sample projects from ptvs

Taking IronPython for a Spin

Finally, what does it mean to say IronPython integrates with the .NET Framework?

Create a new IronPython Application and paste the following Python/.NET amalgamation into “”. Some lines are passing the result of .NET functions to Python and vice-versa… it’s more seamless than I imagined at first.

(The documentation provides more samples to try out, if you’re interested; I’m barely touching the tip of the iceberg here.)

import clr  

# Import some namespaces
from System import Console, Environment, Math, String

# Get absolute value and write to the console with .NET Framework

# Use Python's abs function, then write it out using .NET Framework

# Import an individual method
from System.Console import WriteLine  

from System.Collections.Generic import List  
from System.Linq import Enumerable

names = List[String](["jimmy", "tom", "bill"])

# Count matching names using .NET Framework, then print results using Python
print(Enumerable.Count(names, lambda name : name.Contains("m")))

raw_input("\nPress any key to exit.\n")  

running ironpython code mixed with dotnet framework

That’s it for now. For someone familiar with the .NET Framework, this may save a lot of time and even prevent you from reinventing some wheels (there’s a lot of functionality built into the .NET Framework… almost anything you need is in there).

What do you think? Did you find this useful or interesting? Run into any weird issues? Share below!

Subscribe to Weekly Updates!

Get an email with the latest posts, once per week...
* indicates required