Fix Python – Can I install Python windows packages into virtualenvs?


Asked By – Ned Batchelder

Virtualenv is great: it lets me keep a number of distinct Python installations so that different projects’ dependencies aren’t all thrown together into a common pile.

But if I want to install a package on Windows that’s packaged as a .exe installer, how can I direct it to install into the virtualenv? For example, I have pycuda-0.94rc.win32-py2.6.exe. When I run it, it examines the registry, and finds only one Python26 to install into, the common one that my virtualenv is based off of.

How can I direct it to install into the virtualenv?

Now we will see solution for issue: Can I install Python windows packages into virtualenvs?


I ended up adapting a script ( to register a Python installation in the registry. I can pick the Python to be the Python in the registry, run the Windows installer, then set the registry back:

# -*- encoding: utf-8 -*-
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
# Adapted by Ned Batchelder from a script
# written by Joakim Löw for Secret Labs AB / PythonWare
# source:

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath

def RegisterPy():
        reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
    except EnvironmentError:
            reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
        except Exception, e:
            print "*** Unable to register: %s" % e

    SetValue(reg, installkey, REG_SZ, installpath)
    SetValue(reg, pythonkey, REG_SZ, pythonpath)
    print "--- Python %s at %s is now registered!" % (version, installpath)

if __name__ == "__main__":

Run this script with the Python you want to be registered, and it will be entered into the registry. Note that on Windows 7 and Vista, you’ll need Administrator privileges.

This question is answered By – Ned Batchelder

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0