Skip to main content
 首页 » 编程设计

Python:在两个 python 安装之间共享 python 站点包库

2025年05月04日8zhoujg

我在我的 C: 驱动器上安装了 Python 2.7。然后,我在一台单独的机器上安装 Python 2.7,并将该文件夹复制到我的 J: 驱动器上。如何共享或指向 J: 安装的 python 以使用我的 C: 驱动器站点包包?

即如何在同一台机器上的两个 Python 安装之间共享我的站点包路径?

当我尝试使用 pip 和 virtualenv 安装 wxPython 时,我得到以下信息:

pip install wxPython 
 
Downloading from URLhttp://downloads.sourceforge.net/wxpython/wxPython-src-3.0.2.0.tar.bz2 (from http://wxPython.org/download.php) 
 
Running setup.py egg_info for package wxPython 
 
Traceback (most recent call last): 
 
    File "<string>", line 16, in <module> 
 
IOError: [Errno 2] No such file or directory: '.virtualenvs\\engineer\\build\\wxPython\\setup.py' 
 
Complete output from command python setup.py egg_info: 
 
Traceback (most recent call last): 
 
File "<string>", line 16, in <module> 
 
IOError: [Errno 2] No such file or directory: .virtualenvs\\engineer\\build\\wxPython\\setup.py 

我发现很难在 Windows 上构建和编译,但是 wxPython 安装程序似乎可以与 virtualenv 一起使用。

下面的评论很有用,但是我现在要问的问题是,我几乎拥有 virtualenv 中的所有包,我如何才能引用我无权访问且专有但可以由用户稍后在他们自己的 python 安装中安装?

到目前为止,我偶然发现了 PkgResource api,这可能是我需要的。

请您参考如下方法:

解决用户无权将其包含在 virtualenv 包中的这种外部依赖的方法是使用名为 pkg_resources 的包:

Python代码

# One approach is to find pythonhome by an environment variable 
# Another is to use the windows registry but beware of different 
# issue regarding 64 and 32 bit windows as shown in the link below: 

Python _winreg woes

import pkg_resources 
import os 
pythonhome = os.environ["PYTHONHOME"] 
pkgs = pkg_resources.find_distributions(pythonhome + "/Lib/site-packages") 
for pkg in pkgs: 
    pkg_resources.working_set.add(pkg) 
# import the modules 
# Note: that this does not import the dependencies of the packages imported 
# You could get the dependencies by following the guide here: 

pkg_resource documentation

keeping in mind the following: 
(Naive way of doing this:) 
import pkg_resources 
distributions, errors = pkg_resources.working_set.find_plugins(pkg_resources.Environment(("C:/Python27/Lib/site-packages",))) 
for dsts in distributions: 
    for requirements in dsts.requires(): 
        # load the requirements aswell 
        for requiredDsts in list(distributions): 
            if requirements.project_name == requiredDsts.project_name and  requirements.version >= requiredDsts.specs[0][1]: 
                # the >= may not be whats required as it is specified in specs[0][0] 
                pkg_resources.working_set.add(requiredDsts) 

要使用 virtualenv 进行设置,请按照 (bruno desthuilliers) https://pypi.python.org/pypi/virtualenv/12.0.5 提供的链接给出的说明进行操作。随后为 Windows 用户提供了一个不错的教程 windows tutorial on virtualenv (link provided by bruno desthuilliers) :

  1. 激活当前虚拟环境

  2. 从网站下载并安装 wxPython(它将获取 virtualenv)

  3. 通过 pip 或 easy_install 安装任何其他依赖项

  4. 使用 pkg_resources 执行上述操作,以包含任何无法通过 pip 或 easy_install 安装且用户必须手动安装的软件包。

注意:您只能导入适合 python 解释器本身的包,例如 32 位或 64 位