Scott Hanselman

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

July 2, '13 Comments [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. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Tuesday, July 02, 2013 8:07:38 AM UTC
Mind. Blown. This is just awesome!
Glenn Henriksen
Tuesday, July 02, 2013 9:55:06 AM UTC
Loving this. Can't wait to start Python development with VS!
Pranav Ainavolu
Tuesday, July 02, 2013 10:58:09 AM UTC
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.
Tuesday, July 02, 2013 11:15:43 AM UTC
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.
Scott Stubbs
Tuesday, July 02, 2013 11:44:25 AM UTC
What did you use to install multiple python versions with and manage which ones gets used?
Thanks for the great article!
Shane
Tuesday, July 02, 2013 12:26:22 PM UTC
Too bad there's no similar tooling for Ruby
fm
Tuesday, July 02, 2013 1:09:34 PM UTC
Awesomeness! I can't wait to try it out!
Tuesday, July 02, 2013 1:14:08 PM UTC
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?
Tuesday, July 02, 2013 1:35:44 PM UTC
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*
Alexander
Tuesday, July 02, 2013 2:34:05 PM UTC
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.
Matt Freeman
Tuesday, July 02, 2013 2:56:29 PM UTC
This is very nice! I'll definitely consider this for my upcoming Pluralsight course on Game programming in Python with PyGame!

Thanks Scott!
Tuesday, July 02, 2013 3:06:14 PM UTC
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!
Alex
Tuesday, July 02, 2013 3:06:38 PM UTC
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/
John Goodyear
Tuesday, July 02, 2013 3:14:51 PM UTC
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.
Eduard Florinescu
Tuesday, July 02, 2013 3:17:44 PM UTC
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 ...
Tuesday, July 02, 2013 3:37:15 PM UTC
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! :-)
Tuesday, July 02, 2013 3:46:07 PM UTC
I am waiting for great PHP-Support in VS. Anything to expect on this front?
Mike Devonport
Tuesday, July 02, 2013 3:56:22 PM UTC
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.
Preston Moore
Tuesday, July 02, 2013 4:12:50 PM UTC
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.
FrankenPC
Tuesday, July 02, 2013 4:36:42 PM UTC
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?

Louis St-Amour
Tuesday, July 02, 2013 5:15:25 PM UTC
@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.
Tuesday, July 02, 2013 5:44:14 PM UTC
awesome!
laike9m
Tuesday, July 02, 2013 5:44:28 PM UTC
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!
Tuesday, July 02, 2013 6:21:41 PM UTC
Don't forget about www.pythonchallenge.com to learn the language
Scott Lance
Tuesday, July 02, 2013 6:33:16 PM UTC
As a C# junkie I hate Python but =D>
Amir
Tuesday, July 02, 2013 6:35:12 PM UTC
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).
int19h
Tuesday, July 02, 2013 7:36:32 PM UTC
Merci, j'ai bien ri.
Julien Hautefeuille
Tuesday, July 02, 2013 9:00:10 PM UTC
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.
Tuesday, July 02, 2013 11:08:32 PM UTC
@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.
Tuesday, July 02, 2013 11:35:38 PM UTC
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.
Nameless
Wednesday, July 03, 2013 12:02:15 AM UTC
I really like PTVS. I learned Python using PTVS for my Coursera class.
Debra D.
Wednesday, July 03, 2013 12:33:04 AM UTC
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 :)
int19h
Wednesday, July 03, 2013 2:15:33 AM UTC
Awesome.
John
Wednesday, July 03, 2013 7:32:52 AM UTC
Django is not that massive...
Wednesday, July 03, 2013 11:56:57 AM UTC
This just made my day.
If only I knew about this few months earlier, it could have saved me so much work...
Alex
Wednesday, July 03, 2013 2:27:55 PM UTC
The GIF at the end is the best part!!
Hrusikesh Panda
Wednesday, July 03, 2013 2:47:14 PM UTC
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!
Wednesday, July 03, 2013 3:32:00 PM UTC
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.
Ross Bemrose
Wednesday, July 03, 2013 3:48:23 PM UTC
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.
Wednesday, July 03, 2013 5:46:07 PM UTC
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
NobodySpecial
Wednesday, July 03, 2013 6:24:37 PM UTC
Now all VS needs is to add world class support for java. Compared to VS, eclipse is horrible.
Wednesday, July 03, 2013 6:49:49 PM UTC
Fadi - Of course, it's used by lots. It's not a secret. It's been in development for YEARS.
Thursday, July 04, 2013 1:29:55 PM UTC
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.
Thursday, July 04, 2013 4:56:19 PM UTC
Is it possible to use python and PTVS to develop windows store applications?
Anthony
Saturday, July 06, 2013 8:24:08 AM UTC
Creating Django project requires IIS?
Chatchai Naenudorn
Saturday, July 06, 2013 8:35:48 PM UTC
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.
Saturday, July 06, 2013 10:07:12 PM UTC
Glad To Use Python On MS VS :)
Rahim
Monday, July 08, 2013 11:49:09 AM UTC
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!
Monday, July 08, 2013 3:33:08 PM UTC
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.
Dizzyguy
Tuesday, July 09, 2013 5:42:52 AM UTC
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
Thursday, July 11, 2013 4:41:34 AM UTC
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
Thursday, July 11, 2013 7:28:05 PM UTC
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.

Tuesday, July 16, 2013 6:36:53 PM UTC

@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!
ptools
Tuesday, July 23, 2013 6:15:07 PM UTC
Framework 4.5 only have python support or previous versions also compatible.
Tuesday, July 30, 2013 11:19:17 PM UTC
Visually developing and Python to support Microsoft to thank you.
Monday, August 12, 2013 2:52:13 AM UTC
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
Friday, August 16, 2013 4:47:52 AM UTC
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.
Theo
Friday, August 23, 2013 12:00:55 PM UTC
@Amir: funny you hate python and love C#, did you notice C# 3 brought to .NET devs A LOT of features available in Python?
pete
Tuesday, August 27, 2013 9:23:40 AM UTC
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.
MySchizoBuddy
Thursday, September 05, 2013 7:38:02 PM UTC
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
Thursday, September 05, 2013 7:39:26 PM UTC
@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).
Friday, September 06, 2013 12:38:23 AM UTC
When I try to import libraries like NLTK in the python application, it says unknown type. How do we resolve it? Thanks.
Raj
Wednesday, September 18, 2013 2:40:00 PM UTC
Flask SPA with Knockout.js running in Visual Studio 2013 RC 1 - Booyaaa!

http://twitter.com/_stevenelliott/status/380326810950451200/photo/1
Steven Elliott
Saturday, September 21, 2013 5:22:39 PM UTC
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!
Keith
Thursday, September 26, 2013 2:17:49 AM UTC
Oh, this is great, just loved it! <3 <3 <3
Narankhuu
Thursday, September 26, 2013 5:07:00 AM UTC
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!
Thursday, September 26, 2013 11:01:20 PM UTC
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
Saturday, September 28, 2013 6:30:13 PM UTC
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).
Warren P
Wednesday, October 02, 2013 6:53:45 PM UTC
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.
John Gill
Thursday, October 17, 2013 8:45:47 PM UTC
Anyone knows why VS2012 gets dog slow when editing a .py file ? It's rather annoying.
mab
Sunday, October 20, 2013 11:24:06 PM UTC
this setup was surprisingly easy! Thanks!
Tuesday, October 22, 2013 4:52:15 AM UTC
@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).




kasper o.
Tuesday, October 22, 2013 8:41:52 PM UTC
Is it better to use PVTS+Visual Studio or any specialized software such as Anaconda ?
john
Thursday, October 31, 2013 2:14:35 PM UTC
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?
Joey
Monday, November 11, 2013 5:02:02 PM UTC
Really A nice PlugIn, Just awsome.
Sunday, November 17, 2013 8:02:28 PM UTC
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!
Brandon Tran
Monday, November 18, 2013 6:44:05 PM UTC
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.
Muhannad
Tuesday, November 19, 2013 2:25:48 PM UTC
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).
Dave Semmelink
Comments are closed.

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