There seem to be various editors for Python and there are many articles online (e.g., this blog post) that discuss the features of the various editors. PyCharm by JetBrains seems pretty popular, but while I was Googling for Python editors, I came across Python Tools for Visual Studio. Coming from a C# background, I thought I’d give it a shot before trying out a totally new editor (I’ve moved onto Spyder now though).
The first thing you need to do is download PTVS from CodePlex. I downloaded PTVS 2.1 VS 2013.msi since I’m on VS2013. Of course you’ll need to install Python first if you haven’t done so already – I installed 3.4 (64-bit initially – but had to revert to 32-bit later).
At this point you should be able to create a Python project in Visual Studio – here is a good tutorial on how to create your first Python program in VS. Basically you create a new Python project, very similar to how you would create a .net application.
Creating a new project creates a new .py file with one line of code:
Now you hit F5, it runs your Python code:
This is the easy part. What I was having trouble was figuring out how to add external libraries and import these. Following is the simple Python code I was trying to run (from the Udacity Machine Learning class):
import numpy as np X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) Y = np.array([1, 1, 1, 2, 2, 2]) from sklearn.naive_bayes import GaussianNB clf = GaussianNB() clf.fit(X, Y) print(clf.predict([[-0.8, -1]]))
This is when I started having trouble. VS kept complaining about “No module named ‘numpy’.” and kept stopping at the import statement.
If you google ‘visual studio no module named numpy’ or ‘no module named numpy’ you’ll find tons of threads with various suggestions, including installing/upgrading pip (pip comes with 3.4), using easy_install and running other registry updates. I couldn’t get any of these to work. With pip, it gave me false hope looking as if everything was fine:
but then stopped out with errors (more on installing using command line later).
After lots of searching, I decided to try the numpy Windows installer available at http://www.scipy.org/scipylib/download.html. There’s only 32-bit version available on this official site, so I just tried installing that, but ended up with the following error saying python was not found in the registry:
Here’s what finally worked –
I uninstalled my 64-bit Python and reinstalled Python 32-bit.
Then tried installing numpy – which worked!
Similarly, I could install other modules too. sklearn installed like a charm:
The previous code requires scipy, so I installed scipy too (from http://www.scipy.org/scipylib/download.html):
Now my Python code runs like a charm in Visual Studio! 🙂
So just to recap, these are the steps to follow:
- Install Python 32-bit. Make sure ‘Add python.exe to path’ is enabled.
- Install Python Tools for Visual Studio.
- Install numpy 32-bit and any other external modules you need.
- Run you code!
Seems pretty straight-forward, but lots of people, including myself, seem to have trouble getting Visual Studio to work with Python, especially getting the external modules to work.
By the way, there are ‘unofficial’ versions of numpy available in 64-bit, and also Windows versions of Python available (like IronPython), but I haven’t really played around with these. I’m sure some of these combinations would work equally well.
Lastly, it seems pretty straightforward to install Python modules using pip. Essentially there are (at least?) 3 ways –
– if there’s a zip file (or a tar.gz), simply download and unzip into a folder (it should contain a file called setup.py) – see the YouTube video here. Then go to that folder in a command prompt and do:
python setup.py install
– if you downloaded a .whl file, just open a command prompt and type:
pip install some-package.whl
– install directly using pip (no need to change directory or anything, just open a command window):
pip install –U packageName (e.g., PySide)
You will need to use one of the above two methods to install packages that don’t have the Windows installers. For instance, matplotlib had an installer, but it’s got dependency on six which doesn’t have an installer. Matplotlib also requires dateutil and pyparsing, and http://www.lfd.uci.edu/~gohlke/pythonlibs was a great resource to download these modules from.