简体   繁体   English

使用 pip 安装 package 的多个版本

[英]Installing multiple versions of a package with pip

In my application I would like to use:在我的应用程序中,我想使用:

  • packageA , which requires packageX==1.3 packageA ,需要packageX==1.3
  • packageB , which requires packageX==1.4 packageB ,需要packageX==1.4
  • packageX==1.5

How can I install multiple versions of packageX with pip to handle this situation?如何使用 pip 安装多个版本的packageX来处理这种情况?

pip won't help you with this. pip 帮不了你。

You can tell it to install a specific version, but it will override the other one.您可以告诉它安装特定版本,但它会覆盖另一个版本。 On the other hand, using two virtualenvs will let you install both versions on the same machine, but not use them at the same time.另一方面,使用两个 virtualenv 可以让您在同一台机器上安装两个版本,但不能同时使用它们。

You best bet is to install both version manually, by putting them in your Python path with a different name.最好的办法是手动安装这两个版本,方法是将它们放在 Python 路径中,使用不同的名称。

But if your two libs expect them to have the same name (and they should), you will have to modify them so they pick up the version they need with some import alias such as:但是,如果您的两个库希望它们具有相同的名称(并且它们应该具有相同的名称),您将不得不修改它们,以便它们使用一些导入别名来获取它们需要的版本,例如:

import dependencyname_version as dependencyname

There is currently no clean way to do this.目前没有干净的方法可以做到这一点。 The best you can hope is for this hack to work.你能希望的最好的就是让这个黑客工作。

I'd rather ditch one of the two libs and replace it with an equivalent, or patch it to accept the new version of the dependency and give the patch back to the community.我宁愿放弃这两个库中的一个并将其替换为等效库,或者对其进行修补以接受新版本的依赖项并将补丁返回给社区。

Download the source for ea.下载 ea 的源代码。 package. package。 Install each on its own separate folder.将每个安装在其自己的单独文件夹中。 For example.例如。 I had version 1.10 package, but wanted to switch to the dev version for some work.我有版本 1.10 package,但想切换到开发版本进行一些工作。 I downloaded the source for the dev module: git clone https://github.com/networkx/networkx.git cd netwokrx I created a folder for this version: mkdir /home/username/opt/python , then I set the PYTHONPATH env var to: export PYTHONPATH=/home/username/opt/python/lib/python2.7/site-packages/ . I downloaded the source for the dev module: git clone https://github.com/networkx/networkx.git cd netwokrx I created a folder for this version: mkdir /home/username/opt/python , then I set the PYTHONPATH env var to: export PYTHONPATH=/home/username/opt/python/lib/python2.7/site-packages/ Next, I installed it using: python setup.py install --prefix=/home/username/opt/python接下来,我安装它使用: python setup.py install --prefix=/home/username/opt/python

Now, since my PYTHONPATH is now pointing to this other site-packages folder, when I run python on the command line, and import the new module, it works.现在,由于我的 PYTHONPATH 现在指向另一个站点包文件夹,所以当我在命令行上运行 python 并导入新模块时,它就可以工作了。 To switch switch back, remove the new folder from PYTHONPATH.要切换回来,请从 PYTHONPATH 中删除新文件夹。

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

an ugly 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.我在搅拌机中与 python 一起使用的一个丑陋的解决方法是我将安装(并远离路径)类似版本的 python 并使用子进程让其他版本完成所需的工作。 Blenders python tends to get a little temperamental if you do much more than install pandas and scipy.如果您做的不仅仅是安装 pandas 和 scipy,那么搅拌机 python 往往会变得有点喜怒无常。 I've tried this using virtualenvs with blender but that tends to break things.我已经尝试过使用带有搅拌机的 virtualenvs 但这往往会破坏事情。

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.许多为搅拌机制作插件的人不是“程序员”,所以那些精明的人经常会做一些非常骇人听闻的事情,这是我能够使用的最好的解决方法。

Another workaround (and this has so many flags on the play that it should disqualify me from touching a keyboard) is to manually locate the init file and manually add it to globals with importlib... this comes with risks.另一种解决方法(这在游戏中有很多标志,以至于它应该取消我触摸键盘的资格)是手动定位初始化文件并使用 importlib 手动将其添加到全局变量中......这会带来风险。 Some modules will play alright when you do this, other modules will toss crapfits that can lead to extra special troubleshooting sessions.执行此操作时,某些模块会正常运行,而其他模块会抛出可能导致额外特殊故障排除会话的废话。 Keep it to like versions and it does cut down on the issues and I've had 'alright' luck with using this to import modulus from behind virtual envs, but there is a reason why I use subprocess calls when working with blender's python.保持它喜欢的版本,它确实减少了问题,我很幸运使用它从虚拟环境后面导入模数,但我在使用搅拌机的 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

Another "workaround" is to use IPC/RPC and run isolated packages in services.另一个“解决方法”是使用 IPC/RPC 并在服务中运行隔离包。 If the dependencies are on the different libraries, maybe can separate by the usage of the packages.如果依赖关系在不同的库上,也许可以通过包的使用来分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM