问题:我可以将Python Windows软件包安装到virtualenvs吗?

Virtualenv很棒:它可以让我保留许多不同的Python安装,这样就不会将不同项目的依赖项放在一起。

但是,如果要在以.exe安装程序打包的Windows上安装软件包,如何将其定向安装到virtualenv中?例如,我有pycuda-0.94rc.win32-py2.6.exe。当我运行它时,它会检查注册表,并仅找到一个要安装到我的virtualenv所基于的通用Python26中。

如何引导它安装到virtualenv中?

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?


回答 0

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

easy_install binary_installer_built_with_distutils.exe

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

更新

正如Tritium21在他今天的回答中指出的那样,您应该使用pip代替easy_install。Pip无法安装distutils创建的二进制软件包,但可以在新版本中安装二进制软件包 wheel格式的。您可以使用滚轮套件将旧格式转换为新格式,必须先安装。

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.


回答 1

我知道这是一个很老的问题,并且早于我要谈论的工具,但是对于Google而言,我认为提这个问题是个好主意。easy_install是python包装的败类。没有人愿意承认使用这种新的流行点子。同样,尽管玩注册表技巧对于非标准EXE安装程序最为有效(有人自己安装了安装程序而不是使用distutils,并正在检查注册表中的安装路径),但现在有一种更好的方法用于标准EXE安装程序。

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

在本博文中最近引入的轮式是蛋形的替代品,起着相同的作用。pip(virtualenv中已安装的工具)也支持此格式。

如果由于某种原因pip install WHEELFILE不起作用,请尝试wheel install WHEELFILE

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


回答 2

我最终修改了一个脚本(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()

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

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.


回答 3

easy_install能够安装.exe软件包,只要它们是使用distutils的bdist_wininst目标构建的,该目标涵盖了许多流行的软件包。但是,还有许多其他方面不是(wxPython是我一直在努力的一种)

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)


回答 4

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

dev-env-path/bin/easy_install pycuda

它将为您提供相同的0.94rc版本。

在Windows上,easy_install.exe将位于Scripts目录中。

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.


回答 5

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

如果您使用.exe,我认为没有一种干净的方法。一种选择是使用诸如7Zip(或winzip等)之类的程序直接提取exe的内容,然后将relevent文件夹复制到您的虚拟site-packages文件夹中。例如,如果我解压缩“ processing-0.5.2.win32-py2.5.exe”,则会找到一个文件夹“ PLATLIB \ processing”,将其复制到virtualenv路径并可以使用,而不会出现任何运行时问题。(我不确定这是否总是那么简单。)

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。