You don’t usually think of Microsoft and Python in the same sentence. But now, in Visual Studio 2017, you can develop applications in Python, making Visual Studio probably the best Python IDE around (at least on Windows).
This isn’t just for the Pro or Enterprise versions of Visual Studio; it includes the free Community edition. (There are license restrictions on the Community edition, of course, but individual developers can nonetheless use it to create free or paid applications. Organizations are expected to buy the Pro/Enterprise edition unless it’s for academic research or contributing to open source.)
Is Python Installed?
Visual Studio is component-based, and gives you the choice of installing specific workloads and components. Typical Workloads include ASP.NET and web development, .NET desktop development, and, in this case, Python development.
The default for the Python development workload is Python 3 64-bit, but in the VS 2017 Installer components tab, you can download Python 2 and either (or both) Python 2/ 3 in 32-bit. Other components installed by the Python development workload include Python language support and Python web support. There’s also optional Python IoT support and Anaconda 2 or 3 (again in 32- or 64-bit); you just tick the relevant boxes to have it downloaded and installed.
After you’ve done that, you’ll have a Python entry in the New Project screen, with choices of Web projects (Bottle, Flask, Jade and Django), normal Python and Iron Python, PyGame and Pyvot projects. You’ll also have some machine learning projects, including Classifier, Clustering and Regression.
Debugging in Visual Studio
To be honest, debugging in Visual Studio is no different than debugging in C++, C#, and so on. You run the program with F5, press F10 to step over functions, and F11 to step into them. Just click in the left gutter to add a breakpoint, and execution will stop there.
You have access to all local variables in a debug window; the intermediate window acts as a REPL. Type in the name of a variable to see its value, or a function to call it. Plus, there’s no need to use the traceback module to view a stack trace; it’s there for you anytime in the Call Stack window, and looks like this when it hits the breakpoint on the t = (t * n) line:
> pi in Example1 line 13 Python inner in D:\devstuff\devstuff\dice.com\2018\August2018\VisualStudio-python\Python\Example1\Example1\ line 6 Python Timer.timeit in timeit line 176 Python timeit in timeit line 232 Python Example1 module line 19 Python
Just to test it, I timed this program to calculate the first 1,000 digits of Pi. It took just over a second to do it 100 times:
import decimal import timeit def pi(): decimal.getcontext().prec += 2 three = decimal.Decimal(3) lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 while s != lasts: lasts = s n, na = n + na, na + 8 d, da = d + da, da + 32 t = (t * n) / d s += t decimal.getcontext().prec -= 2 return +s decimal.getcontext().prec = 1000 print(timeit.timeit(pi,number=10)) print(pi())
In addition to the Intermediate Window, which only exists while stepping, there’s also a full interactive REPL window that is closer to the standard python REPL. It’s not displayed by default, but can be found on the View/Other Windows menu. You can specify that your applications run under it.
One of Python’s biggest strengths is the ecosystem of packages; the website Pypi currently lists nearly 150,000. To get the best out of them, though, you need to use virtual environments so the packages and dependencies can be managed on a per-project basis.
Visual Studio provides you with a global environment, a virtual environment using pip, or a Conda environment using Anaconda (yes, that’s a lot of choices, but thankfully it’s all well-documented). The default environment is Python 3 – (32-bit), which contrasts with Python 3 – (64-bit), which was the only Python installed in Visual Studio.
In Solution Explorer, you can search Pypi to find, download and install packages into a specified environment in a project. It’s similar to the process with NuGet, though here there’s no command line commands; it’s all in the Visual Studio GUI.
One of Visual Studio’s strengths is IntelliSense, and they haven’t neglected it with Python. Move the cursor over a variable, function or module name to see popup information about it, particularly parameters for functions.
Microsoft supports the cross-platform Visual Studio Code and recently created a Python language server. Visual Studio Python IntelliSense has been refactored into a separate plugin for Visual Studio Code that provides all the information so the editor can do tooltips and completions, locate definitions and references, and rename global variables.
I was very impressed with what Microsoft has done; the ability to directly import packages from Pypi into any environment is neat. I recommend a read of Microsoft’s Work with Python in Visual Studio article if you want additional details; it offers a matrix of what Python features can be used with earlier versions of Visual Studio, going back as far as Visual Studio 2010. You need to install it in those earlier versions; there’s another Microsoft article on doing just that.
I think Microsoft has done an excellent job in integrating Python into Visual Studio; the documentation is great. It’s clearly by far the best IDE on Windows for Python, and free.