0

I need help with the next situation. There is one project, that is requiring two versions of one library. Let this lib be lib, and its versions: libold and libnew. These libs are not accessible via pypi, i.e. they are each in their own folder. Let the paths of these folders be /path/to/libold and /path/to/libnew.

In my project I need instances of classes from both these libs, but I can't import them both, but only either old or new lib.
I tried the next method:

import sys
sys.path.insert(0,'path/to/libold')
import lib as libold
sys.path.pop(0)
sys.path.insert(0,'path/to/libnew')
import lib as libnew

After performing this commands, libold and libnew represents the same library, libold.

I also tried importlib and imp and got same result.

How can I perform importing 2 versions of a lib?

FOX 9000
  • 123
  • 1
  • 1
  • 6
skeeph
  • 462
  • 2
  • 6
  • 18
  • why dont you import only the classes that you need? – R Nar Nov 05 '15 at 21:02
  • By "lib" do you mean a python module or a C extension? Is it a package with many .py files? – tdelaney Nov 05 '15 at 21:03
  • Have you tested that `sys.path.insert(0,'path/to/libnew');import lib as libnew` actually imports the new version? No offense intended. It looks to me like your code should work, so I just want to be sure. – saulspatz Nov 05 '15 at 21:21
  • @RNar classes, that i need have same names in both libs – skeeph Nov 05 '15 at 22:19
  • @tdelaney actually this is package of py files and C extensions. This is caffe lib https://github.com/BVLC/caffe – skeeph Nov 05 '15 at 22:20
  • @skeeph what did the attempts using importlib look like? – Sebastian Nov 05 '15 at 23:14
  • @Sebastian, it was libold=importlib.import('/path/to/libold'), but i get error ImportError: Import by filename is not supported. – skeeph Nov 06 '15 at 10:19
  • @skeeph look at http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path for how to import based on a path – Sebastian Nov 06 '15 at 11:59
  • @Sebastian i already look at this question, but this is slightly different situation. I need import 2 versions of the **same** library – skeeph Nov 07 '15 at 15:22
  • 1
    This sounds like an xy problem.... Why do you need 2 versions of the same library in the first place? What is the *exact* problem are you trying to solve by doing so? – Sayse Dec 31 '15 at 15:54

1 Answers1

0

Python adds imported modules to sys.modules. When you write import lib as libnew, sys.modules['lib'] already exists, and therefore the new lib is not imported.

To import the new lib, you should delete the old one from sys.modules, like this:

import sys
sys.path.insert(0, 'path/to/libold')
import lib as libold
sys.path.pop(0)
del sys.modules['lib']
sys.path.insert(0, 'path/to/libnew')
import lib as libnew

However, you may encounter serious problems by doing so. In particular, if the old lib tries to import a submodule (say, e.g. lib.submodule), it will get the new one instead. For this reason, you'd better import all submodules of the old lib before deleting sys.modules['lib'] and before importing the new one.

However, that's a dirty hack, not a real solution. In Python, modules and packages are identified by name, not by path. This is how it works and has always worked, and there's nothing you can do about it.

Consider using multiprocessing instead to overcome these "limitations". With multiprocessing you can run two processes: one that uses the old lib and the other that uses the new one. multiprocessing gives you many tools to make interprocess communication easy.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69