简体   繁体   English

python:运行时带有pkg_resources的多个库版本(pkg_resources.VersionConflict错误)

[英]python : multiple library versions at run time with pkg_resources (pkg_resources.VersionConflict error)

I'm trying to use two different releases of a same library (installed with easy_install --multi-version) from within the same python script. 我正在尝试从相同的python脚本中使用同一库的两个不同版本(随easy_install --multi-version安装)。 The general idea is illustrated in the code below. 下面的代码说明了总体思路。

If I call each version independently, everything is fine. 如果我分别调用每个版本,一切都很好。 If I want to call one version and then the other, I get a VersionConflict error. 如果要先调用一个版本,然后再调用另一个版本,则会收到VersionConflict错误。

There must be a way to "unload" the previous distribution from the working set before loading the other one but I seem to always get lost reading the pkg_resources manual . 在加载另一个之前,必须有一种方法可以从工作集中“卸载”先前的发行版,但是我似乎总是迷路于阅读pkg_resources手册

Can anyone point me to the right way to go about this? 谁能指出我正确的解决方法? Thanks a lot. 非常感谢。

#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-
def test1():
    import pkg_resources
    pkg_resources.require('obspy.core==0.6.2')
    import obspy.core
    try:
        print obspy.core.__version__
    except:
       print "Can not read obspy.core version"
def test2():
    import pkg_resources
    pkg_resources.require('obspy.core==0.4.8')
    import obspy.core
    try:
        print obspy.core.__version__
    except:
        print "Can not read obspy.core version"
if __name__ == '__main__':
    test1()
    test2()

Have you tried to use reload(module) in the cases where the module is already imported? 在已经导入模块的情况下,您是否尝试过使用reload(module)

Reload 重装

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

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