Install NumPy in a Virtual Environment on Windows

Posted by in Software

Someone told me they use a custom python install named Anaconda on windows because it contains a set of math packages which are difficult to install. In fairness, they’re right because any python module with native dependencies is a mess to install from sources (e.g. because you need the same version of Visual Studio used to build the python install).

When I tried to tell them the benefits of using virtual environments (which are created clean by default), I’ve been asked to try and install numpy in a virtual environment.

determined-challenge-accepted-l

Prerequisites

First, let’s assume the following:

  • The OS is Windows (Win7 in my case, but the differences should be minor);
  • You have installed Python 2.7 and setuptools, pip and virtualenv successfully;
  • Create a virtual environment (e.g. creating a virtual environment in “C:\Proagam Files (x86)\Python\virtualenvs” named “numeric” would create a new directory with the relevant stuff “C:\Proagam Files (x86)\Python\virtualenvs\numeric”)
  • Activate the virtual environment
  • Set any proxy if needed

Installing NumPy

Once you have performed all of the above, there are two ways to install numpy:

  1. If you have development tools
  2. If you don’t :)

NumPy from Source Code

If you have all relevant developer tools like Visual Studio (C++) and Intel’s libraries, then all you need to do is install numpy from source code:

Numpy from Binaries

If you prefer not to get your hands dirty with compiling stuff, then you can install from a pre-compiled distribution. The steps I used are enumerated below:

  • Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/;
  • Download the relevant library. Don’t install it just yet;
  • Open the binary (setup file) with 7zip. It has 2 directories: PATLIB and SCRIPTS;
  • Copy the contents of “PLATLIB” (the numpy directory and the numpy-1.x.x-py…info into your virtualenv’s \Lib\site-packages directory (e.g. “C:\Program Files (x86)\Python\virtualenvs\numeric\Lib\site-packages”);
  • Copy the contents of “SCRIPTS” into your virtualenv’s \Scripts directory (e.g. “C:\Program Files (x86)\Python\virtualenvs\numeric\Scripts”);
  • Test the installed numpy with something like:

It’s more manual but that’s because the setup executable doesn’t allow you to set up a custom path (for example the binary install of CX_oracle allows you to do that and you can just run the installer).

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)