Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
demonguy Avatar asked Mar 20 '15 06:03

demonguy


1 Answers

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.

like image 194
Klaus D. Avatar answered Sep 28 '22 19:09

Klaus D.