voidynullness

(mis)adventures in software development...

    
19 June 2014

How to install a Python development environment on Windows

Share
Category Python

How to install Python, setuptools, pip, virtualenvwrapper for PowerShell, and PySide on Windows.

This is a brief outline of the process I used recently for setting up a Python development environment on a new Windows 8.1 machine. (Based mostly on information in these posts)

Installing Python itself on Windows — just to run Python scripts — is fairly straightforward: pick a Python implementation (CPython or one of the alternatives) download and run the installer, and you should be good to go.

But to do any kind of non-trivial development, you’re probably going to want a few more tools available — at the very least things like pip and virtualenv.

Since there is a really nice PowerShell implementation of virtualenvwrapper, I like to set things up so I can work from a PowerShell command line.

Here’s what I installed, in the order I installed it, to get a reasonably useable Python development environment on Windows.

Python

First step is to download and install Python.

I went with what I suppose is the conservative approach: 32-bit CPython 2.7. (Yeah, I know…)

After installing Python, make sure running scripts is enabled in PowerShell:

Set-ExecutionPolicy RemoteSigned

If the Python installer didn’t add the location of Python to the system path, you will need to do that yourself. I did that by creating file called profile.ps1 in ~/Documents/WindowsPowerShell and added the Python install path to $env:Path. My profile.ps1 looks like this:

$env:Path += ";C:\Python27;C:\Python27\Scripts"
Import-Module virtualenvwrapper

Modify the path as necessary for your system.

The second line, importing virtualenvwrapper, is explained below.

Setuptools

Next install Setuptools, a tool for building and distributing Python packages. It’s required by pip (see below). In the past we might have installed Distribute instead, but apparently Distribute has been merged into Setuptools, so now Setuptools is the way to go.

Download ez_setup.py and run it in an administrator PowerShell console.

pip

pip is a tool for installing and managing Python packages. It automates and simplifies the process of downloading and installing packages from online repositories (usually PyPI).

To install pip, download get-pip.py.

Then run it in an (administrator) PowerShell console:

python .\get-pip.py

virtualenv and virtualenvwrapper

Different Python project are likely to require different collections of packages. It’s common practice to avoid installing packages into the system Python installation, and instead use virtualenv to create isolated Python environments. virtualenvwrapper simplifies the use of virtualenv for common tasks, and there is a PowerShell port that works really well on Windows.

With setuptools and pip installed, we can now install virtualenv and virtualenvwrapper by using the following commands at a PowerShell prompt:

pip install virtualenv
pip install virtualenvwrapper-powershell
mkdir ~/.virtualenvs

To check if everything installed correctly:

Import-Module virtualenvwrapper
Get-Command *virtualenv*

This should produce a list of available virtualenv commands.

You should now be able to use virtualenvwrapper in pretty much the same way as on Linux (but there’s also PowerShell style cmdlets as alternatives).

To avoid having to type Import-Module virtualenvwrapper every time you open a new PowerShell window, it’s a good idea to add it to your profile, as shown in the earlier example.

Qt and PySide

My reasons for even bothering to install a Python development environment on a Windows machine usually have something to do with Qt based development. I use the PySide bindings, which on Windows can be installed in a virtualenv like so:

New-VirtualEnvironment pyside
easy_install -U PySide

The first line creates a virtualenv called “pyside”, and the second line installs the PySide bindings.

(The easy_install command didn’t work first time I tried it, not sure why. But it ran fine when I tried it again, after a reboot.)


 

Comments