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?
- You can consider using python virtual environment.
- Activate the required environment and run your code there.
You have to import it from one level higher:
import version1.my_lib as a
import version2.my_lib as b
Also make sure that in all the folder there is a __init__.py
.
来源:https://stackoverflow.com/questions/29160625/how-to-import-two-versions-of-the-same-python-module-at-the-same-time