我可以在virtualenv中安装Python windows包吗?

时间:2023-01-12 19:52:47

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.

Virtualenv非常棒:它允许我保留一些不同的Python安装,这样就不会将不同项目的依赖项都堆在一起。

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.

但是,如果我想在Windows上安装一个打包为.exe安装程序的包,我如何将它安装到virtualenv中呢?例如,我有pycuda-0.94rc.win32-py2.6.exe。当我运行它时,它检查注册表,并且只找到一个要安装到的Python26,这是我的virtualenv的基础。

How can I direct it to install into the virtualenv?

如何将其安装到virtualenv中?

6 个解决方案

#1


198  

Yes, you can. All you need is

是的,你可以。你所需要的是

easy_install binary_installer_built_with_distutils.exe

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?

惊讶吗?它看起来像是用distutils合成的Windows的二进制安装程序。exe和。zip合并成一个。exe文件。将扩展名更改为.zip以查看它是一个有效的zip文件。我在阅读了我的问题的答案后发现了这个问题,我在哪里可以下载带有psycopg2的二进制鸡蛋?

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.

正如Tritium21在他的回答中指出的,现在应该使用pip而不是easy_install。Pip不能安装由distutils创建的二进制包,但它可以以新的wheel格式安装二进制包。您可以使用轮包将旧格式转换为新的格式,您必须先安装它。

#2


69  

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.

我知道这是一个相当古老的问题,而且早于我将要讨论的工具,但是为了谷歌,我认为提它是一个好主意。easy_install是python打包的害群之马。没有人愿意承认使用它与匹普的新热。另外,虽然使用注册表技巧对于非标准的EXE安装程序(有人自己构建了安装程序,而不是使用distutils,并且正在检查注册表的安装路径),但是现在有了一个更好的方法(c)用于标准的EXE安装程序。

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).

最近在本文中介绍的滚轮格式是对鸡蛋格式的替换,其作用几乎相同。pip(已经安装在virtualenv中的工具)也支持这种格式。

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

如果由于某种原因pip安装车轮文件不工作,请尝试安装车轮车轮文件

#3


40  

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:

最后我修改了一个脚本(http://effbot.org/zone/python-register.htm),在注册表中注册一个Python安装。我可以选择Python作为注册表中的Python,运行Windows安装程序,然后将注册表设置回:

# -*- 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.

使用要注册的Python运行此脚本,它将被输入注册中心。注意,在Windows 7和Vista上,您需要管理员权限。

#4


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)

easy_install能够安装.exe包,只要它们是使用distutils的bdist_wininst目标构建的,该目标包含许多流行的包。然而,还有很多其他的东西不是这样的(wxPython是我一直纠结的一个)

#5


0  

You can use environment's easy_install to install PyCUDA.

您可以使用environment的easy_install来安装PyCUDA。

dev-env-path/bin/easy_install pycuda

it will give you the same version 0.94rc.

它会给你同样的版本0.94rc。

On Windows easy_install.exe will be in Scripts directory.

在Windows easy_install。exe将在脚本目录中。

#6


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.

如果是.msi,您可以使用msiexec指定命令行选项。Python安装程序本身允许TARGETDIR,但我不确定distutils是否会将其安装到发行版安装程序中。

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.)

如果你用的是。exe,我不认为有什么干净的方法。一种选择是使用程序7Zip(或winzip等)直接提取exe的内容,然后将相关文件夹复制到虚拟站点包文件夹中。例如,如果我提取“processing-0.5.2.win32-py2.5”。exe,我找到了一个文件夹“PLATLIB\processing”,我将它复制到virtualenv路径中,并在没有任何运行时问题的情况下使用。(我不确定事情总是这么简单。)

#1


198  

Yes, you can. All you need is

是的,你可以。你所需要的是

easy_install binary_installer_built_with_distutils.exe

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?

惊讶吗?它看起来像是用distutils合成的Windows的二进制安装程序。exe和。zip合并成一个。exe文件。将扩展名更改为.zip以查看它是一个有效的zip文件。我在阅读了我的问题的答案后发现了这个问题,我在哪里可以下载带有psycopg2的二进制鸡蛋?

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.

正如Tritium21在他的回答中指出的,现在应该使用pip而不是easy_install。Pip不能安装由distutils创建的二进制包,但它可以以新的wheel格式安装二进制包。您可以使用轮包将旧格式转换为新的格式,您必须先安装它。

#2


69  

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.

我知道这是一个相当古老的问题,而且早于我将要讨论的工具,但是为了谷歌,我认为提它是一个好主意。easy_install是python打包的害群之马。没有人愿意承认使用它与匹普的新热。另外,虽然使用注册表技巧对于非标准的EXE安装程序(有人自己构建了安装程序,而不是使用distutils,并且正在检查注册表的安装路径),但是现在有了一个更好的方法(c)用于标准的EXE安装程序。

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).

最近在本文中介绍的滚轮格式是对鸡蛋格式的替换,其作用几乎相同。pip(已经安装在virtualenv中的工具)也支持这种格式。

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

如果由于某种原因pip安装车轮文件不工作,请尝试安装车轮车轮文件

#3


40  

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:

最后我修改了一个脚本(http://effbot.org/zone/python-register.htm),在注册表中注册一个Python安装。我可以选择Python作为注册表中的Python,运行Windows安装程序,然后将注册表设置回:

# -*- 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.

使用要注册的Python运行此脚本,它将被输入注册中心。注意,在Windows 7和Vista上,您需要管理员权限。

#4


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)

easy_install能够安装.exe包,只要它们是使用distutils的bdist_wininst目标构建的,该目标包含许多流行的包。然而,还有很多其他的东西不是这样的(wxPython是我一直纠结的一个)

#5


0  

You can use environment's easy_install to install PyCUDA.

您可以使用environment的easy_install来安装PyCUDA。

dev-env-path/bin/easy_install pycuda

it will give you the same version 0.94rc.

它会给你同样的版本0.94rc。

On Windows easy_install.exe will be in Scripts directory.

在Windows easy_install。exe将在脚本目录中。

#6


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.

如果是.msi,您可以使用msiexec指定命令行选项。Python安装程序本身允许TARGETDIR,但我不确定distutils是否会将其安装到发行版安装程序中。

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.)

如果你用的是。exe,我不认为有什么干净的方法。一种选择是使用程序7Zip(或winzip等)直接提取exe的内容,然后将相关文件夹复制到虚拟站点包文件夹中。例如,如果我提取“processing-0.5.2.win32-py2.5”。exe,我找到了一个文件夹“PLATLIB\processing”,我将它复制到virtualenv路径中,并在没有任何运行时问题的情况下使用。(我不确定事情总是这么简单。)