Search code examples
pythonpython-importpython-module

How to import two versions of the same python module at the same time?


Suppose I have two versions of a python package, say "lib". One is in folder ~/version1/lib and the other is in ~/version2/lib. I'm trying to load both packages in one session by doing this:

sys.path.insert(0, '~/version1')
import lib as a

sys.path.insert(0, '~/version2')
import lib as b

But it doesn't work, because of cache, b will be the same as a.

Is there anyway to do it? Maybe use hook in sys.meta_path? I didn't figure it out.

Or is there anyway to delete cache of imported modules?


Solution