124

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?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    the answer below works for me http://stackoverflow.com/questions/6114115/windows-virtualenv-pip-numpy-problems-when-installing-numpy – user1960422 Jan 23 '15 at 03:58

7 Answers7

201

Yes, you can. All you need is

easy_install binary_installer_built_with_distutils.exe

Surprised? It looks like binary installers for Windows made with distutils combine .exe with .zip into one .exe file. Change extension to .zip to see it's a valid zip file. I discovered this after reading answers to my question Where can I download binary eggs with psycopg2 for Windows?

UPDATE

As noted by Tritium21 in his answer nowadays you should use pip instead of easy_install. Pip can't install binary packages created by distutils but it can install binary packages in the new wheel format. You can convert from old format to the new one using wheel package, which you have to install first.

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • 2
    I agree, this is great. By the way, if you have 7zip, you can open the .exe directly, without having to rename it as zip. – Sabuncu May 02 '12 at 16:12
  • 5
    just a tip: developers can download various built python packages for windows from http://www.lfd.uci.edu/~gohlke/pythonlibs/ . – yangzh Sep 07 '13 at 04:29
  • @CodyHatch You can as this is not my only answer on SO and there's a chance some of my other answers would be helpful to you as well :) – Piotr Dobrogost Nov 26 '13 at 16:43
  • 1
    I get the following: Searching for binary-installer-built-with-distutils.exe Reading http://pypi.python.org/simple/binary_installer_built_with_distutils.exe/ Reading http://pypi.python.org/simple/binary-installer-built-with-distutils.exe/ Couldn't find index page for 'binary_installer_built_with_distutils.exe' Scanning index of all packages (this may take a while) Reading http://pypi.python.org/simple/ No local packages or download links found for binary-installer-built-with-distutils.exe error: Could not find suitable distribution for Requirement.parse('binary-instal ler-built-with-d... – Paul Jun 09 '14 at 15:31
  • 1
    Paul, the exe he's referring to will be different depending on which installer you're using...not literally "binary-installer-built-with-distutils.exe" – Tristan Reid Jun 09 '14 at 17:28
  • How would one do this on OSX? – AllTradesJack Nov 03 '14 at 17:56
  • it should be pointed out that there are some serious drawbacks to using easy_install, including security, the inability to use wheels (the new format that actually is a standard), increased import time (eggs do some whacky things - installing a lot of them slows down the import process dramatically.) – Tritium21 Jan 07 '15 at 16:14
71

I know this is quite an old question, and predates the tools I am about to talk about, but for the sake of Google, I think it is a good idea to mention it. easy_install is the black sheep of python packaging. No one wants to admit using it with the new hotness of pip around. Also, while playing registry tricks will work best for non-standard EXE installers (someone built the installer themselves instead of using distutils, and is checking the registry for the installation path), there is now a Better Way(c) for standard EXE installers.

pip install wheel
wheel convert INSTALLER.EXE
pip install NEW_FILE_CREATED_IN_LAST_STEP.whl

The wheel format, introduced recently as of this post, is the replacement for the egg format, filling much the same role. This format is also supported by pip (a tool already installed in your virtualenv).

if for some reason pip install WHEELFILE does not work, try wheel install WHEELFILE

Tritium21
  • 2,845
  • 18
  • 27
  • Thanks for this. Hopefully more people notice your answer and upvote! – Brett Stottlemyer Jan 06 '14 at 13:45
  • To be clear. You can create a .whl from a .exe, for example from http://www.lfd.uci.edu/~gohlke/pythonlibs/, with this. This is HUGE im terms of deployment! – Jonas Gröger Apr 06 '14 at 10:51
  • I tried to do this, but got an error wheel convert numpy-MKL-1.8.1.win-amd64-py2.7.exe then pip install numpy-1.8.1-cp27-none-win_amd64.whl i get the following error Downloading/unpacking numpy-1.8.1-cp27-none-win-amd64.whl Could not find any downloads that satisfy the requirement numpy-1.8.1-cp27-non e-win-amd64.whl No distributions at all found for numpy-1.8.1-cp27-none-win-amd64.whl – Paul Jun 09 '14 at 14:44
  • try ``wheel install WHEELFILE``, answer edited to point out the existence of that. Debugging a specific problem for someone is not something trivially done in SO comments. – Tritium21 Jun 09 '14 at 23:06
  • After needing easy_install so many times for only this purpose, that was beautiful. *tearfully waves goodbye to easy_install – KobeJohn Oct 19 '14 at 08:22
41

I ended up adapting a script (http://effbot.org/zone/python-register.htm) 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:
# http://www.pythonware.com/products/works/articles/regpy20.htm

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():
    try:
        reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
    except EnvironmentError:
        try:
            reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
        except Exception, e:
            print "*** Unable to register: %s" % e
            return

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

if __name__ == "__main__":
    RegisterPy()

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    On Windows 7, 64 bit, with 64 bit Python had to modify regpath to `regpath = "SOFTWARE\\Wow6432Node\\Python\\Pythoncore\\%s\\" % (version)` – Oleksiy May 25 '14 at 05:59
  • Actually for my windows 7 , 64 bit, I left the regpath as is and it worked. The mod @Oleksiy suggested did not work. Probably some subtle different in our OS version, or some setting. – Paul Jun 09 '14 at 15:20
  • @Ned Batchelder's solution was the only one that worked for me. The wheel installation did not work. I still received package download errors (see my comment on that answer for more detail). The suggestion to do the easy_install binary_installer... may have worked but I was not able to get easy_install to find the binary_installer...exe file. – Paul Jun 09 '14 at 15:23
7

easy_install is able to install .exe packages as long as they were built using distutils' bdist_wininst target, which covers many popular packages. However, there are many others that aren't (wxPython is one that I've struggled with)

Simon King
  • 228
  • 1
  • 4
  • Yes, and as long as you've installed whatever development package of all the dependencies. In my case pycuda needs Boost and CUDA, not a trivial undertaking. – Ned Batchelder Jul 18 '10 at 13:03
  • 1
    @Ned Batchelder *Yes, and as long as you've installed whatever development package of all the dependencies.* Well, binary installer doesn't install all dependencies neither, does it? – Piotr Dobrogost Mar 26 '11 at 12:46
0

You can use environment's easy_install to install PyCUDA.

dev-env-path/bin/easy_install pycuda

it will give you the same version 0.94rc.

On Windows easy_install.exe will be in Scripts directory.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73
0

If it's a .msi, you might be able to specify command line options using msiexec. The Python installer itself allows TARGETDIR, but I'm not sure if distutils bakes this into distribution installers.

If you're using a .exe, I don't think there's a clean way. One option is to use a program like 7Zip (or winzip, etc) to directly extract the contents of the exe, then copy the relevent folders into your virtual site-packages folder. For example, if I extract "processing-0.5.2.win32-py2.5.exe", I find a folder "PLATLIB\processing" which I copy to a virtualenv path and use without any runtime problems. (I'm not sure it's always that simple though.)

ars
  • 120,335
  • 23
  • 147
  • 134
0

You should type path of your file and write 'python ' before it.

Than it will run your python script without any virtual environment.

Thanks.