Scott Hanselman

One of Microsoft's Best-Kept Secrets - Python Tools for Visual Studio (PTVS)

July 02, 2013 Comment on this post [78] Posted in Open Source | Python
Sponsored By
Python Tools for VS
 

I've talked some about the sweet support for node and PHP in Azure. You can also File | New | Node.js express application in WebMatrix, or run WordPress and get intellisense as well.

"I installed windows just so i can use PTVS" - Comment on Hacker News

But I'm consistently shocked that folks forget about Python at Microsoft. I am a C# person, myself, but the Developer Division at Microsoft loves their languages. C++, VB, C#, F#, etc and they aren't messing about when they get serious about a language.

One of the least-known and most-kick-butt free products we have is PTVS - Python Tools for Visual Studio. Whether you're just interested in learning Python or you're a hardcore PhD who wants mixed-language Python and C++ debugging or somewhere in between, you gotta check this out. (Seriously, the mixed-mode debugging thing can't be overstressed...)

The Misconceptions

  • Microsoft? Python?  Oh, it must be all about IronPython, that's dead, right?
    • IronPython is a community-run project and just put an 2.7.4 alpha out last month.  PTVS fully supports IronPython, but the most advanced support is for standard CPython!
  • PTVS needs VisualStudio? I don't have any money.
    • PTVS, combined with the Integrated/isolated VS Shell is completely and perpetually free.  And with the advent of VS2013, they've combined them into a single installer: https://pytools.codeplex.com/releases (at bottom of page).

This is Real

Here's my VS2013 after installing PyTools (PTVS). I've got IronPython which is Python running under the .NET CLR, but I've also got Django apps as well as a regular CPython or making a new project from existing code.

Python inside VS

You can see that PTVS knows what Python engines I have installed, and I can easily switch between them. Here you can see that VS is refreshing the auto-completion (intellisense) databases for each version.

A list of Python Interpreters

There's also a complete REPL inside Visual Studio for each:

Python REPL inside VS

Developing Django Apps in Visual Studio

Maybe you're a Django (one of Python's Web Frameworks) web developer, you can use VS to develop your app.

Go File New | Django App, then make a new Python Virtual Environment from the Solution Explorer, and watch Visual Studio freaking installed pip for you (the Python package manager). It's very seamless.

Adding a Virtual Python Environment

Which gives me this:

Python in my VS and I'm FREAKING OUT

Then I right click on "dev" and just like NuGet (except this is Python, so pip) I install django:

Installing Django

Django is massive, so this took a while, but still! And.....I've accomplished Hello World in Django. Well, Hello Django, at least, launched from Visual Studio.

Hello Django

You should feel free to go and run through the whole Django Tutorial if you like and even deploy your app to Azure! You can host Django on a regular Azure Web Site, or a Virtual Machine if you want more control.

You can even interactively debug Python running in Azure on Linux from your Visual Studio instance! Check out Steve do just that at PyCon in this YouTube video.

There's a bunch of great educational and quick start Tutorials on the Python Tools YouTube Channel, they are a great resource to bookmark.

You can attach to remote Python processes over SSL and debug if you like.

Setting up Python Debugging

It's Really Integrated

Let's get real here for a second. Lots of projects plug stuff into Visual Studio. You may have made it this far into the post and be saying "oh, wah wah, this thing sets up some batch files and some syntax highlighting and calls itself a full-featured Python IDE."

Um, no. This is the best of VS and the best of Python and I'm blown away. Check this out. PTVS knows that I'm doing unit testing here and they've integrated Python Unit Testing with the VS Unit Testing UI.

Unit Testing in Python and VS? My heart can't take it!

This is debugging, remote debugging, cross language debugging, tool tips, watches, locals, call stacks, unit testing, full REPL with inline graphics, profiling, cloud publish, best of class CPython support, and so much more.

Nailed it

If you're into Python or knows someone who is, for reals, drink it in and get on board at https://pytools.codeplex.com. Check out their samples. They've got Python talking to Kinect, Python talking to Excel and more. Their PTVS Documentation is really good as well.

Just getting started? Well, go Learn Python The Hard Way.

Installing PTVS

Here's the complete install instrucitons. You need VS, the PTVS, and some Python.

PTVS is free

Finally, explore the Resources and Docs for Python Tools for Visual Studio, including, but not limited to Editing, Refactoring, Unit Tests, Django, IPython notebook and Azure cloud computing, Kinect for Python and Pyvot - an Excel to Python bridge.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 02, 2013 12:07
Mind. Blown. This is just awesome!
July 02, 2013 13:55
Loving this. Can't wait to start Python development with VS!
July 02, 2013 14:58
This is awesome!
I am just getting into python and being a c# developer I know that no one makes better dev tools than microsoft.
July 02, 2013 15:15
Wow! I'm impressed the integration is so polished. Thank you for highlighting this. Python is a fun language and having good tooling should make it more fun.
July 02, 2013 15:44
What did you use to install multiple python versions with and manage which ones gets used?
Thanks for the great article!
July 02, 2013 16:26
Too bad there's no similar tooling for Ruby
fm
July 02, 2013 17:09
Awesomeness! I can't wait to try it out!
July 02, 2013 17:14
I was aware of IronPython and the VS Tools but I fell into the category of many who though the tools were just for IronPython. Do you know if the VS Tools for IronRuby are similar in that they provide support for actual Ruby as well as IronRuby?
July 02, 2013 17:35
You are aware that Kane is "applauding" for an absolute disaster that has happened before his eyes for which he's burned quite a lot of money?

*scnr*
July 02, 2013 18:34
Funny you didnt mention the firing/reallocation of py staff in MS from 2010 and how the project dragged for years, pretty sure you did in a podcast at the time.
July 02, 2013 18:56
This is very nice! I'll definitely consider this for my upcoming Pluralsight course on Game programming in Python with PyGame!

Thanks Scott!
July 02, 2013 19:06
I've been using this for a few months now, and it's really changed how I feel about using Python. I'm excited for v2.0 to get out of beta, so we can use the VS unit testing framework with Python.

It works really well for mixed-language solutions, too!
July 02, 2013 19:06
What's the relationship with IIS? Would this allow me to build a web app in Python and deploy easily to IIS? I've been investigating doing this with: http://www.helicontech.com/zoo/
July 02, 2013 19:14
I used PTVS combined with the Integrated VS Shell and is a good way to develop freely.

Although you can also use SharpDevelop with Ironpython which can also help make independent executable files.
July 02, 2013 19:17
See, I love when you find the hidden gems in Microsoft and highlight them.

It may be hard to see from within our .NET/Ruby/Node.JS walled Ivory Garden, but Python is a BIG deal. Genomic mapping and analysis, BioStatistics, Bioinformatics, manufacturing, game development, etc ...
July 02, 2013 19:37
Fantastic !

The support for developers from Microsoft has been phenomenal, but I really love the way how more and more open source technologies are coming into the mix, off late! :-)
July 02, 2013 19:46
I am waiting for great PHP-Support in VS. Anything to expect on this front?
July 02, 2013 19:56
I've been meaning to start doing more Python development recently. This might just get me to start again. I just wish that I had known about this last year when I took my Web Development class, and had to build a Django website.
July 02, 2013 20:12
Awesome. Coincidentally, I just discovered the Arduino plug in to VS. It's not that I CAN'T program outside the VS framework...it's that I don't want to. It's time for everyone to admit that Microsoft pile drove Borland into the ground and emerged victorious with the supreme development environment.
July 02, 2013 20:36
I'm thrilled by VS 2013's new features and seeing this Python support. But I'll echo what's been said above: somebody needs to make basic Java, Ruby and other languages available. I'd do it myself if there was an easy step-by-step. I'd particularly want to get Google Go and Dartlang running, particularly with the new TFS-powered (?) CodeLens functionality.

I suppose, as a starting point: Does CodeLens work with PTVS?

July 02, 2013 21:15
@JohnGoodyear I find that for the most part "integrated" solutions such as IIS-Node, and if there is similar for Python are probably more complicated than needed, and likely not as good as simply installing and using the Application Request Routing (ARR) module for IIS. You can then run your Python/Ruby/NodeJS app with Non-Sucking Service Manager (NSSM) for windows.

In essense you write your app as any other ruby/python/nodejs app, you create a simple cmd/bat file that launches your "webserver" in that platform.. then use nssm to register that command as a windows service (say on port 8001).

From there, you configure IIS to act as a reverse-proxy to your app that runs on a different port connecting www.yourapp.com to localhost:8001.
July 02, 2013 21:44
awesome!
July 02, 2013 21:44
Thanks, the timing could not have been better for this post. I have to say I completely forgot about Python at MS, since all IronX went away...

I am just starting a MOOC and was wandering which tools I would use for my Python programming. For those who love Math, learn Linear Algebra through Python programming at https://www.coursera.org/course/matrix. The teacher is really passionate, that's the least I can say!
July 02, 2013 22:21
Don't forget about www.pythonchallenge.com to learn the language
July 02, 2013 22:33
As a C# junkie I hate Python but =D>
July 02, 2013 22:35
Hi guys, I am a developer on PTVS team. Thanks for all the great feedback! Now, to answer some of your questions...

>> What did you use to install multiple python versions with and manage which ones gets used?

The "Python Interpreters" window that is on the screenshot is a part of PTVS. It doesn't really help you to install them other than directing you to the documentation page explaining what options are there and where to get them, but it's the one you use to see what you have, configure them, choose the one to use by default, open REPLs for them, and monitor progress when PTVS is refreshing code completion database.

>> Does CodeLens work with PTVS?

It doesn't (this functionality is not something that we get automatically - it'd have to be specifically implemented for Python). That said, if you want us to do it, create a feature request at our issue tracker - we do take these very seriously when planning work for new releases (indeed, if you look at what we added in 2.0, it was the three top voted features from our tracker, and two more features that were in top 10 by votes).

>> What's the relationship with IIS? Would this allow me to build a web app in Python and deploy easily to IIS?

You can use the Deploy functionality with local IIS, but the web.config that it'll generate by default is designed for the Azure environment. You can override that by adding your own web.config to the project, but then the burden is on you to configure it for your Python environment (which shouldn't be hard to do if you take the default config and tweak it).
July 02, 2013 23:36
Merci, j'ai bien ri.
July 03, 2013 1:00
I love PTVS. It's hands down the best Python IDE I've tried. I'm a fan of IPython Notebook as a REPL, but for building real software, PTVS rocks.
July 03, 2013 3:08
@John Goodyear

A little more on IIS - We created our own Python script for doing a WSGI<->FastCGI gateway for IIS (it's called wfastcgi). That works w/ normal IIS, IIS Express, and Windows Azure Web Sites (where it's already deployed on all of the servers along w/ Python 2.7). It's also available on our downloads page.

It's in PTVS too because within VS we support deployment to Azure where we'll automatically package this up and generate the appropriate web.config to setup and run. There's a tutorial on how to set it up on Azure and the same basic appcmd's will work off Azure to setup normal IIS.

Basically this accomplishes the same thing as Zoo but is much simpler. It has no dependencies beyond a standard Python install and is just a single script that can easily be copied anywhere. Also my initial benchmarks seemed to show this as faster but they certainly weren't exhaustive and YMMV.
July 03, 2013 3:35
The VS version of PyCharm (or IntelliJ with Python plugin)
PyCharm is cross-platform, though, and I have trouble imagining that even VS can compete.
July 03, 2013 4:02
I really like PTVS. I learned Python using PTVS for my Coursera class.
July 03, 2013 4:33
Nameless, try this in PyCharm:

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()]

And see what member completion options it gives you for a, b, c and d. Then do the same in PTVS :)
July 03, 2013 6:15
Awesome.
July 03, 2013 11:32
Django is not that massive...
July 03, 2013 15:56
This just made my day.
If only I knew about this few months earlier, it could have saved me so much work...
July 03, 2013 18:27
The GIF at the end is the best part!!
July 03, 2013 18:47
I'm a long time C#/VS developer, and one of the suckers accepting the Google Glass Explorers invite. I was considering doing my app development in Python - I've never used it, but have wanted to take a peek since it's been suggested as a good language for introducing Geeklings to programming. The fact that I can use an IDE with which I'm comfortable makes it a little less intimidating to pick up a new platform AND a new language at the same time.

Thanks for the info, and keep up the (always excellent and entertaining) work!
July 03, 2013 19:32
I've heard about PyTools before, but more as an example of how to write things using VS's language tools since I don't actually know Python.

I haven't looked at it for a few years, though... does the 2.0Beta make use of any VS2012 or 2013 features?

Unfortunately, using it an example of language tools kinda failed as it was for a non-work project that I kinda let die because it ended up looking like it was going to be a much larger project than I initially thought.
July 03, 2013 19:48
Hi Scott,

Does anyone actually use PyTools in the real world? And if yes, then why?

Maybe there's a reason why MS kept it a secret.
July 03, 2013 21:46
Not sure if it's a bug/feature.

After installing this and running VS2012 unit tests for native C++ project the testrunner spends 20-30secs "loading symbols..." for a bunch of python libs - even when there is no python in the project
July 03, 2013 22:24
Now all VS needs is to add world class support for java. Compared to VS, eclipse is horrible.
July 03, 2013 22:49
Fadi - Of course, it's used by lots. It's not a secret. It's been in development for YEARS.
July 04, 2013 17:29
This is really cool. I was doing some experiments with IronPython and PythonStudio about 2 years ago and really liked it.
Then I lost track of these project and assumed they were dead by now.
Thanks for bringing them back on the table.
The quality and functionality of this VS integration blows my mind.
July 04, 2013 20:56
Is it possible to use python and PTVS to develop windows store applications?
July 06, 2013 12:24
Creating Django project requires IIS?
July 07, 2013 0:35
Well, i have to say the last place i would expect python support is on vs, this changes the impression i have about microsoft, perhaps they should be more vocal about their work.
July 07, 2013 2:07
Glad To Use Python On MS VS :)
July 08, 2013 15:49
Very cool Scott... I have wanted to learn Python and this gives me a reason to do so since i'm in VS 24/7 lol. I agree with Radu, we just need VS to do Java.. and I'd be in heaven!
July 08, 2013 19:33
I've programmed C# for 10+ years, but our group just moved to Python/Django. Glad to see you take notice, Scott - hopefully a lot more managers at Microsoft do too. You see, now I don't have to leave my favorite environment - Visual Studio - which is why I'm so glad to see you giving these guys credit! If not for them, I'd be on a Mac with PyCharm, but now I get to stay in Windows and Visual Studio writing Django apps + Typescript, and can still O&M all of my .NET apps - in one place. Seriously, I love to see that Microsoft is getting behind these guys, they deserve more credit and support.
July 09, 2013 9:42
Interesting article but unfortunately, not relevant... until you posted that little gem at the end "Learn Python the Hard Way". That's a fun little introduction to the language and gave me a chance to run through the editor. You're a master at finding interesting stuff on outskirts of the web. Thanks for sharing. :-)
Dan
July 11, 2013 8:41
I am using beta 2.0 on vs 2012;
too bad it hangs on large python projects:
OpenERP

the previous version for vs 2010 works but realy slow when first opening this large projects
July 11, 2013 23:28
This is nice if all you're coding Python for is Windows. Unfortunately, there is a much larger world out there. Microsoft really needs to port Visual Studio to Mac (at minimum) and eventually Linux (gasp!) for this to appeal to the larger Python community.

For now, I'm going to stick with vim, Sublime, PyCharm, IPython, etc.

July 16, 2013 22:36

@indra - this issue has been repro'd & fixed already for RC (late summer). there is also a work around:

http://pytools.codeplex.com/workitem/1476

thanks!
July 23, 2013 22:15
Framework 4.5 only have python support or previous versions also compatible.
July 31, 2013 3:19
Visually developing and Python to support Microsoft to thank you.
August 12, 2013 6:52
Has anyone been able to get pip to install when behind a firewall? NuGet and all the other things in VS2012 works fine, but when i try to get pip to go, it gives me a 407 proxy authentication required error. I added HTTP_PROXY as an Environment variable, but still no joy.
Rob
August 16, 2013 8:47
Never wanting to leave any level of interest and skill out: I would add, for beginners/students, this link: http://interactivepython.org/courselib/static/thinkcspy/toc.html
This site has blown me away - much as your post did.
August 23, 2013 16:00
@Amir: funny you hate python and love C#, did you notice C# 3 brought to .NET devs A LOT of features available in Python?
August 27, 2013 13:23
You should clarify whether the screenshots you posted is for PTVS with isolated VS Shell. Or is it VS2013 FULL with PTVS. It isn't clear to me.
September 05, 2013 23:38
Regarding the proxy problem mentioned above. The workaround is to install pip manually, after which you can add proxy to its config file, as described here:

https://pytools.codeplex.com/discussions/453117

We're also tracking the feature request to add proxy support to pip bootstrapping:

https://pytools.codeplex.com/workitem/1626
September 05, 2013 23:39
@negaboys: You do not need .NET to run Python applications at all. As for PTVS, it has the same requirement as the version of VS it is targeting (and if you have the corresponding VS installed, you already have the right version of .NET).
September 06, 2013 4:38
When I try to import libraries like NLTK in the python application, it says unknown type. How do we resolve it? Thanks.
Raj
September 18, 2013 18:40
Flask SPA with Knockout.js running in Visual Studio 2013 RC 1 - Booyaaa!

http://twitter.com/_stevenelliott/status/380326810950451200/photo/1
September 21, 2013 21:22
Is it possible to get a django application running on IronPython? I've been trying unsuccessfully to get this working for a couple days now.

It seems like IronPython has enough subtle differences that running a high complexity application like django is difficult.

If django apps could be portable to IronPython that seems like it would be a crazy win for Microsoft: All you would need is ASP.NET hosting and you could host both .NET and Django projects!
September 26, 2013 6:17
Oh, this is great, just loved it! <3 <3 <3
September 26, 2013 9:07
Great article! I was actually shocked when I saw the support for Python in Visual Studio. It was enough to get me wanting to pick up python again. I actually blogged about my first experiences with it: http://www.devleader.ca/2013/09/23/visual-studio-c-python-sweet/

Thanks for the info!
September 27, 2013 3:01
PythonToolsIntegrated.exe failed to install on Windows 7 (without Visual Studio 2013), not sure if I need to install VS 2013 first (I don't want to).
ex
September 28, 2013 22:30
I am not impressed. I was expecting a REPL built into the IDE, but when I run my python script, a CONSOLE window launches, and my output goes into that console. (It is also scraped and written back to the IDE). I was thinking that there would be an interactive Python output window with a REPL (python prompt).
October 02, 2013 22:53
A great alternative to IronPython is pythondotnet: http://pythonnet.sourceforge.net/

This allows you to use .NET libraries directly from CPython. You can do stuff like this:


from System.Collections import Hashtable
table = Hashtable()
table["key 1"] = "value 1"


The advantage over IronPython is you also have all python C-extensions available. Rumour has it that you can also call C-python from .NET with this goodness, but I have not tried that.
October 18, 2013 0:45
Anyone knows why VS2012 gets dog slow when editing a .py file ? It's rather annoying.
mab
October 21, 2013 3:24
this setup was surprisingly easy! Thanks!
October 22, 2013 8:52
@mab

When I ran it with VS2012 there was a (pytools?) process rapidly eating memory even after I closed VS, don't know if it is connected to your problem though. I downloaded VS2013 ult RC from dreamspark and it ran alot smoother (though now, after I had to reinstall all my programs, when I try to download pytools I get an incompatibility error with VS2013 RC, maybe I just need to search a bit better).




October 23, 2013 0:41
Is it better to use PVTS+Visual Studio or any specialized software such as Anaconda ?
October 31, 2013 18:14
python 3.3 is compiled with VS2010, many modules downloaded with pip from pypi look for VS2008. If I use VS2013 (without the other version installed) is this a problem? Any suggestions how to get around it?
November 11, 2013 21:02
Really A nice PlugIn, Just awsome.
November 18, 2013 0:02
Awesome! I already consider VS to be the best IDE out there. Now that it can integrate Python, it is undefeatable!

Now I'm wondering if there is some sort of plug-in for writing Java/Android apps in there too...Hmmmmm....

Thanks Scott!
November 18, 2013 22:44
Visual Studio is a great IDLE & PTVS is certainly a great choice for working with IronPython, but the best Python IDLE has to go to JetBrains for PyCharm, that's one intelligent IDLE.
November 19, 2013 18:25
I installed PTVS and IronPython, and created a Windows Forms app. It runs fine, so now I need to add some controls to the form. I had no idea how to do this, so I pressed F1 hoping to get some help, and the VS2012 IDE crashed (Not Responding).

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.