在虚拟环境中使用不同版本的相同库 [英] Using same library with different version across virtual environment

查看:28
本文介绍了在虚拟环境中使用不同版本的相同库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 开发一个项目,该项目在多个模块中具有依赖项.

I am working on a project in python which have dependencies in multiple modules.

举个例子,我的主模块使用另外2个模块,即module_1,它需要库lib_version_1.5module_2strong> 使用相同的 library_but_version_1.8 .

As an example say my main module uses another 2 modules namely module_1 which needs library lib_version_1.5 and module_2 which uses the same library_but_version_1.8 .

module_1 和 module_2 都运行良好,现在我正在创建另一个结合这两者的模块.

Both module_1 and module_2 is running fine, now I am creating another module combining these two.

那么我如何使用虚拟环境,以便我可以将这两者与所有必需的库(不同版本)结合起来?是否可以在具有不同子模块的项目中安装同一库的不同版本?

So how can I use virtual environment so that I can combine these two with all required libraries (different versions) ? Is it possible to get different version of the same library installed within a project having different sub modules?

推荐答案

请注意,python 命名空间非常强大.希望这个小例子能帮到你.

Note that the python namingspace is quite powerful. Hope this small Example will help you.

基本原理是将不同版本的相同libs添加到python的类路径中,并使其名称不同,以免发生覆盖.

The basic theory is that add the same libs with different versions into the python's class path and make them be different names so that there will not be overriding.

import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libs'))

上面的代码将在你的项目基目录下的'libs'目录下插入第三方库.

The above code will insert the third-part libs which under the directory of 'libs' that under your project's base directory.

假设我需要 2.4.1 和 2.6.1 版本的 pycrypto,使用 pip install pycrypto==2.6.1 -t ./libsip install pycrypto==2.4.1 -t ./libs 将两个版本安装到项目下的子目录中.安装后修改他们的名字,否则会覆盖现有的.

Suppose that I need pycrypto with version 2.4.1 and 2.6.1, use pip install pycrypto==2.6.1 -t ./libs and ip install pycrypto==2.4.1 -t ./libs to install the two versions into a sub-directory under your project. Modify their name after you have installed one, otherwise it will override the existed one.

将它们的名称分别修改为 Crypto241 和 Crypto261.

Modify their names to Crypto241 and Crypto261 seprately.

假设我将有两个模块(ModuleWIthCrypto241ModuleWithCrypto261),它们将分别导入 pycrypto 2.4.1 和 2.6.1.

Let's say I will have two modules(ModuleWIthCrypto241, ModuleWithCrypto261) which will import pycrypto 2.4.1 and 2.6.1 seprately.

ModuleWithCrypto261 带代码:

ModuleWithCrypto261 with code:

def getCryproVersion(baseDir):
    import os, sys
    sys.path.insert(0, os.path.join(baseDir, 'libs'))
    import Crypto261
    return Crypto261.__version__

ModuleWIthCrypto241 代码:

ModuleWIthCrypto241 with code:

def getCryproVersion(baseDir):
    import os, sys
    sys.path.insert(0, os.path.join(baseDir, 'libs'))
    import Crypto241
    return Crypto241.__version__

几乎一样,只是导入不同版本的pycrypto.

Almost the same, just import the different version of pycrypto.

然后我们有另一个脚本尝试获取两个不同版本的 pycrypto.modulewithdiffversion.py

Then we have another script which try to get the two different version of pycrypto.modulewithdiffversion.py

它的样子:

import os, sys

from com.x import ModuleWIthCrypto241
from com.x import ModuleWithCrypto261
if __name__ == '__main__':
    baseDir = os.path.dirname(os.path.abspath(__file__))
    print ModuleWIthCrypto241.getCryproVersion(baseDir)
    print ModuleWithCrypto261.getCryproVersion(baseDir)

执行main方法,会打印出pycrypto的两个版本:

Execute the main method, it will print out the two versions of the pycrypto:

2.4.1
2.6.1

希望对你有帮助!

这篇关于在虚拟环境中使用不同版本的相同库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆