Install Python, Pip and Virtualenv on Windows

Installing Python on Windows isn’t rocket science. Here are 3 easy steps to get a complete Python environment on a Windows machine.

1: Install Python

Chocolatey users can install Python2.7 with cinst python. This will install either the 32-bit or 64-bit build, depending on your version of Windows. Keep in mind that the 64-bit version might cause troubles with third party modules. Chocolatey will also add the Python directory into the Systems Path variable, so you can already move on to step 2.

For non-Chocolatey users, here is the manuel procedure:

The installer for Python is available on Python.org. See the Quick Links on the left hand side. I recommend to take the 32-bit version. As you might run into issues with third party modules if you use the 64-bit version instead.

Whether you take Python3 or Python27 depends mostly on the third party libraries you’re going to use. I recommend to take python 3 unless you know that there is a library that isn’t yet compatible that you’ll want to use.

Since Python 3.3 and upwards there is an option in the install wizard to include the python.exe in the global PATH variable which makes the python.exe executable available in all directories. By default that option is disabled but I recommend to enable it.

To add python.exe manually to the PATH variable hit the Windows + Break key to get to the System Overview, hit Advanced System Settings on the left hand side. Then in the Advanced register, click on the Environment Variables Button. Here you should see under System variables the Path variable. Add C:\Python32 (or whatever Path it is you installed Python to) and head on to step 2.

2: Install Pip

If you’re new to Python you might wonder what this Pip thing is.

You might have seen instructions that told you to install XY by using

pip install packageXY

Pip is a package manager for python that can be used to easily install (almost any) python library.

Think of apt-get but tailored especially for Python. Or if you’re coming from a .NET area, think of it as Nuget.

If you’ve installed Python 3.4 you can skip this step and the next as Python 3.4 already includes pip (unless you de-selected it in the install wizard).

Before you can install Pip, you’ll need setuptools or distribute. If you’re using Python3, you must use distribute as setuptools doesn’t support Python 3.x

To install distribute download the setup file and invoke it using python.

python.exe C:\Path\to\distribute_setup.py

Now that distribute is installed, Pip can also be installed. Download get-pip.py and invoke it in the same way you invoked distribute_setup:

python.exe c:\Path\to\get-pip.py

After that Pip is installed. But you might want to add C:\Python32\Scripts to the Path Systemvariable too (see step 1). So you can execute pip.exe from any location.

3: Virtualenv

What does virtualenv do? It creates isolated Python environments. Instead of installing every Python package system-wide you can install version 1.1 into the test1 sandbox, version1.2 into the test2 sandbox and so on.

Not only that, but it is also possible to have multiple python versions available. So you can have Python26, Python27 and Python32 installed all into different sandboxes. This is very handy if you want to test if a python script you wrote runs under multiple versions.

python 3.4

If Python 3.4 is installed it is not necessary to install virtualenv separately. Instead it is possible to use the venv module:

python.exe -m venv venvname

python < 3.4

virtualenv can be installed using the previously installed pip:

pip.exe install virtualenv

After virtualenv has been installed it is possible to create a virtualenv like this:

virtualenv.exe venvname

This will create the venvname virtualenv in the current directory. By default it includes a copy of Pip that can be used to install additional libraries within the sandbox.

But first the environment needs to be activated using the activate script located in .\venvname\Scripts\.

venvname\Scripts\activate

More on virtualenv is out of the scope of this post, as it is really immensely powerful. For more information visit the virtualenv page.

4: (Bonus) Install python modules that need a compiler

Not all python libraries are purely python. Some use extensions like Cython include C code. To install these libraries a compiler is required. Without a compiler, pip will spit out something among the lines of unable to find vcvarsall.

It is possible to use either the compiler included in Visual Studio or MinGW. I’ll focus on MinGW. Download the installer from the above link and run it. At least install the C Compiler. If you want to play it save, also add the C++ Compiler.

Once installed. Add C:\MinGW\bin to the Path Systemvariable.

Now the only thing left to do is to tell distribute to use the MinGW compiler (by default it will try to use the C++ Compiler from Visual Studio). In the Lib\distutils directory within your virtualenv create (or edit if it already exists) the distutils.cfg file and add:

[build]
compiler=mingw32

Once you’ve done that, you should be able to install python modules that require a compiler. You can test it by installing cython.

pip install cython

One issue I’ve run into is that MinGW doesn’t recognise the -mno-cygwin flag anymore. To resolve that I simply removed the flag in C:\Python27\Lib\distutils\cygwincompiler.py line 322 to 326.

You can of course save yourself all this trouble if you use prebuild packages. But keep in mind that installers will always install the packages system-wide and not into a specific virtualenv.

That’s about it. Have fun playing around with python!

Updated (2014-05-06)

Updated the post to also contain python 3.4 information.

Sunday, June 17, 2012 » Python Programming