This article is written by Pavel Minaev int19h - the developer from the PTVS team specifically for publication in our corporate blog on Habrahabr. Share your impressions in the comments. All feedback will be given to the team.
Hello! I am a developer from the
Python Tools for Visual Studio team. The other day we released a
new beta version of our product, and taking advantage of the occasion, this time I would like to tell more in detail about what PTVS is and what it can offer you.
What is PTVS?
In short, Python Tools for Visual Studio (hereinafter referred to as PTVS), as the name implies, is a free extension for Visual Studio 2010 and higher, which adds full Python support to this IDE. Putting it yourself, you get editing code on Python with backlighting and advanced auto-completion, code navigation, refactoring, debugging, profiling, and Django support with the ability to publish Windows Azure websites in two clicks. It supports all editions of Visual Studio, allowing the installation of extensions - as paid Professional, Premium and Ultimate, and free
Shell . In combination with the latter, PTVS is a full-fledged free development environment for Python under Windows - and in this version we have made a
convenient combined installer for VS Shell + PTVS. If you have already set yourself Visual Studio 2013 Preview, then in the New Project dialog you will have a pleasant surprise:
')
Our project is in some way unique to Microsoft. Open source (under Apache License 2.0) will not surprise anyone now, but we not only
publish the source code on CodePlex, but also invite everyone to
work together on the project . Yes, we accept third-party code!
And finally, since this moment most often causes confusion: PTVS is not IronPython, and this is not an environment focused exclusively on IronPython. We support almost all Python implementations to one degree or another - CPython, IronPython, Jython, PyPy, Stackless - but the priority is to support the standard, and used by most of the Python developers, the CPython interpreter.
Next, I will look at a number of the most interesting and unique features of PTVS in more detail.
Work with code
Perhaps, for any developer, first of all it is the convenience of working with the code that is decisive in choosing an IDE. That is why the advanced engine for parsing the Python code - which implements auto-completion, refactoring, symbolic search and other key features - was the first thing that was created in the framework of the PTVS project. With each new version, we improved the quality of parsing, and beta 2.0 is no exception.
A simple auto-completion for dynamically-typed languages is no longer surprising anyone today, but many editors are sticking to even trivial examples of using the dynamic capabilities of a language. Take, for example, the following code:
def f(x): def g(y): return x + y return g a = f(1)(2) b = f(3.0)(a) c = f(u'a')(str(b)) d = (a, b, c)[input()]
Since the addition operator in Python is polymorphic, the type of the value returned by g - and, as a result, the type of values in a, b, and c - depends solely on the types of arguments passed. Type d is the most complicated because it is indirectly dependent on the types of all three previous variables - the editor must first be assembled and then the tuple disassembled.
PTVS tries to unambiguously determine the type for each particular call, taking into account the previously derived types of arguments passed. Here are the hints and additions that are given in the editor for the various variables in this code:
As you can see, in the latter case, the variable has several potential types, and autocompletion displays them all.
The result of type inference can be viewed on the other hand, if you request a hint for the function parameter in its body:
All types of arguments that the function is called anywhere in the code are collected here — autocompletion for them in the function body will work accordingly. This has an interesting consequence - if you use TDD, and first write tests for your functions and classes, and then implement them, then you will automatically receive the correct auto-completion for all parameters.
In addition to auto-completion, the search for ads and variable references also works. Here, PTVS has one interesting addition - if the variable that started the search refers to (or can refer to) functions or classes, then in the search results, in addition to the line to which this variable was assigned a value, there will also be original declarations of these functions and classes, even if the value was not assigned directly. For example, if you add the following lines to the code above:
h = f if input() else f(a) print h(b)
and execute the Find All References command on h, we get the following result:
As you can see, the function g appears in the list, since it was it that was returned by the call f (a). Of course, this is also reflected in the results of autocompletion when trying to call h - in the proposed signature options all known options for f and g will appear:

Debugging Mixed Code
Exclusive in beta 2.0 - this is no longer anywhere. This is the case when a picture is worth a thousand words. Here she is:
In the screenshot, a debugger attached to a C # program that loads CPython, which in turn loads a plug-in written in C ++, and all these three things call each other in a rather confusing way that can be explored in the window call stack. Of course, you can create breakpoints in all projects, but the most important thing that gives such a combined mode is the possibility of step-by-step debugging of interlanguage calls. For example, if you execute the Step In command on a Python code line that calls a class method implemented in C ++, you will be taken to the source of this method. Similarly, it works for C ++ or C # code that calls PyObject_CallObject for a function written in Python. Calls to native code in ordinary dynamic libraries via ctypes are also supported.
Although the screenshot shows simultaneous debugging of three languages, in practice most often the case is limited to two - Python and C ++ (or C) - either this is a Python program that uses extension modules, or a C ++ program that uses Python as a scripting language. For this reason, we added a number of features to the debugger that are available only for C / C ++. Pay attention to the Locals window in the screenshot, and a detailed representation of the value of the a_natobj variable, which refers to an object of a class implemented in C ++: simultaneously with the Python representation of the object, it also shows C ++, the structure representation that implements its class. There is also a mirror image of Python objects from C ++: this is what the same Locals window looks like in the frob function:
As you can see, the values of variables declared in C ++ as PyObject (or some other standard type from the Python API) display the Python representations of these objects simultaneously with the fields of the corresponding C-structures.
Debug REPL
Having a normal REPL for Python is standard in the IDE for it, and PTVS is no exception - of course, we also support syntax highlighting and auto-completion (the latter, if possible, works directly with the values in memory to give accurate hints for them) . But we went further, and made support for the “live” REPL when debugging code — that is, the REPL works directly on data in the process being debugged, allowing you to calculate complex expressions on the fly and redefine classes and functions. Here is a screenshot of the debug REPL for the first example from this article:
Django and Windows Azure
PTVS supports creating web applications on Django, providing syntax highlighting, code completion, and debugging for display templates:
And, starting with this version of PTVS, applications created in this way can be published to a website in
Windows Azure with ease that was previously only available for ASP.NET:
Et voilà!
Yes, Azure websites now
support Python ! You can also use other Azure features — table service, blob service, service bus, storage queues, etc. - in your Python applications using the official
Windows Azure SDK for Python , which our team also works on. At the same time, the SDK is cross-platform: the only requirement for it is Python 2.6 or higher.
Profiling
Again, the screenshots will say much more than words:
… and much more!
PTVS capabilities are not limited to the above - we have other interesting features, such as refactoring, working with virtualenv and package management via pip and easy_install, remote debugging of programs on Linux and OS X,
IPython support in the REPL (with visualization of graphs), and debugging and profiling of Python code on MPI clusters. You can read about them in more detail on
the project documentation page , or watch an
overview video .
Your feedback is very important to us
No, seriously, it really matters! We have an
open bug tracker , and we are always happy to see in it bug reports and suggestions from users. As for the proposals - this is not an empty phrase. When we start working on a new version, and decide what exactly it will be in, then the first thing we do is open the feature requests list in the tracker, sort by user voices, and see what happens at the top. So, in version 2.0, we implemented five features from the top 10 by votes, including three that were in the first three places. If you just want to ask a question or discuss something, you can do it in our
forum . And, of course, I will be happy to answer any of your questions in the comments to this article.