Using Visual Studio 2013 for Python (3.4) with NumPy and SciPy on Windows


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.

image

Creating a new project creates a new .py file with one line of code:

image

Now you hit F5, it runs your Python code:

image

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:

image

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:

image

Here’s what finally worked –

I uninstalled my 64-bit Python and reinstalled Python 32-bit.

Then tried installing numpy – which worked!

image

Similarly, I could install other modules too. sklearn installed like a charm:

image

The previous code requires scipy, so I installed scipy too (from http://www.scipy.org/scipylib/download.html):

image

Now my Python code runs like a charm in Visual Studio! 🙂

image

So just to recap, these are the steps to follow:

  1. Install Python 32-bit. Make sure ‘Add python.exe to path’ is enabled.
  2. Install Python Tools for Visual Studio.
  3. Install numpy 32-bit and any other external modules you need.
  4. 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.

Advertisements
This entry was posted in General and tagged , , , , , , . Bookmark the permalink.

3 Responses to Using Visual Studio 2013 for Python (3.4) with NumPy and SciPy on Windows

  1. Pingback: Launching Spyder on Windows | Thusitha Mabotuwana's Blog

  2. Anonymous says:

    Great article man. Helped me a ton after spending hours of trying to make NumPy work with a script instead of a console window. I can tell this works with Visual studio 2015 as well.

  3. Anonymous says:

    Thanks a lot!
    I didn’t need the windows installer for numpy, by the way.
    just doing pip install –U numpy on the command line already worked.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s