4

I'm trying to use two different releases of a same library (installed with easy_install --multi-version) from within the same python script. 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.

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.

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()
Oliver Henriot
  • 181
  • 1
  • 10

1 Answers1

0

Have you tried to use reload(module) in the cases where the module is already imported?

Reload

StefanE
  • 7,578
  • 10
  • 48
  • 75
  • As in `reload(pkg_resources)`? Yes, I still get VersionConflict. Am I doing it wrong? Should I reload obspy also? In which order? – Oliver Henriot Mar 07 '12 at 15:10
  • No, thank you for suggesting it Stefan but it still doesn't work. – Oliver Henriot Mar 08 '12 at 10:21
  • What if you place the different imports in different modules? With sys.getrefcount(pkg_resources) you can see if anything still are using the object and if thats the case then reload will keep some "old" stuff and cause problem – StefanE Mar 08 '12 at 12:11
  • I still get the same error even if I place each import in its own module. I don't quite get what `sys.getrefcount(pkg_resoursces)` shows me: when I import pkg_resources, the count is 3, after I import obspy, the count is still 3, if I reload pkg_resources, the count is 7. It seems not only that nothing is unloaded but rather that it's loaded "on top" of itself. Thanks for all your help nonetheless, I'd be nowhere without it ;) – Oliver Henriot Mar 09 '12 at 08:29