使用 pip 安装包的多个版本

在我的申请中,我想使用:

  • packageA,这需要 packageX==1.3
  • packageB,这需要 packageX==1.4
  • packageX==1.5

如何使用 pip 安装多个版本的 packageX来处理这种情况?

62029 次浏览

pip won't help you with this.

您可以告诉它安装一个特定的版本,但它会覆盖另一个版本。另一方面,使用两个 viralenv 可以让您在同一台机器上安装两个版本,但不能同时使用它们。

You best bet is to install both version manually, by putting them in your Python path with a different name.

但是,如果您的两个库希望它们具有相同的名称(它们应该具有相同的名称) ,则必须对它们进行修改,以便它们能够使用一些导入别名来获得所需的版本,例如:

import dependencyname_version as dependencyname

目前还没有一种干净的方法可以做到这一点。

我宁愿丢弃这两个库中的一个,用一个等价的替换它,或者修补它以接受新版本的依赖项,并将修补程序交还给社区。

下载 ea 的源代码。包裹。将每个文件夹安装在它自己的单独文件夹中。比如说。我有版本1.10的软件包,但想切换到开发版本的一些工作。我下载了 dev 模块的源代码: git clone https://github.com/networkx/networkx.git cd netwokrx I created a folder for this version: 然后将 PYTHONPATH env 变量设置为: export PYTHONPATH=/home/username/opt/python/lib/python2.7/site-packages/

现在,由于我的 PYTHONPATH 现在指向这个其他站点包文件夹,当我在命令行上运行 python 并导入新模块时,它就可以工作了。要切换回来,请从 PYTHONPATH 删除新文件夹。

>>> import networkx as nx
>>> nx.__version__
'2.0.dev_20151209221101'

an 丑陋 workaround I use with python in blender is I'll install (and keep off path) a like version of python and use subprocess to have the other version do the needed work. Blenders python tends to get a little temperamental if you do much more than install pandas and scipy. I've tried this using virtualenvs with blender but that tends to break things.

Also on the off chance you are using blender for data visualization, you are going to want to add a config folder to your version number folder, this will keep all of your addons in that folder and it makes it far more portable and far less likely to mess up other installs of blender. Many people who make addons for blender are not 'programmers', so often those savvy people will do some very hackish things and this has been the best workaround I've been able to use.

另一个变通方法(这个方法有太多的标志,它应该取消我触摸键盘的资格)是手动定位 Init文件,然后手动将其添加到 import lib 的全局文件中... ... 这是有风险的。当您这样做时,一些模块将正常运行,其他模块将抛出垃圾,这可能导致额外的故障排除会话。保持它喜欢的版本,它确实减少了问题,我已经有’好的’运气,使用这个导入模从后面的虚拟 envs,但有一个原因为什么我使用子进程调用时,搅拌器的 Python。

def importfromfilelocation(x,y,z):
#"""x = 'tk',y = "tkinter", z =r'C:\pyth"""
mod_alis = x
spec = importlib.util.spec_from_file_location(y, z)
print(spec)
mod_alis = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod_alis)
globals()[str(x)]= mod_alis

另一个“解决方案”是使用 IPC/RPC 并在服务中运行独立的包。如果依赖关系在不同的库上,也许可以根据包的使用情况进行分离。