I have come across two Python modules that have to be imported using the same module name, e.g.
import foo
I know that the one I want provides certain functions (e.g. foo.bar()
), so is there a way to cycle through the modules with the same name until I find the one that provides those functions? Or is there no way around other than renaming the module before installing?
Edit: just to clarify what I mean, both modules are inside site-packages:
site-packages$ ls python_montage-0.9.3-py2.6.egg
EGG-INFO montage
site-packages$ ls montage-0.3.2-py2.6.egg/
EGG-INFO montage
Since python 2.5 (and PEP 328) absolute import is the prefered way of managing modules in python. In your case, your module tree is certainly like this :
If you want to use the foo module with the bar method, use this :
Here is a way :
I bet there is some corners that i didn't take in consideration but hopefully this can give you some idea.
N.B: I think the best idea will be to just rename your module if possible.
N.B 2: As i see in your edited answer, sadly this solution will not work because the two modules exist in the same directory (site-packages/).
If you're happy that you can figure out the filenames through some other heuristic, then you should be able to use imp.load_module to load them separately.
See http://docs.python.org/library/imp.html#imp.load_module
In your case, although both eggs are on the python path, I believe eggs do some magic whereby each egg acts as a path. You might be able to set the egg file as the path argument to
imp.find_module
in order to load them separately.There are ways to hack around the two modules with the same name limitation, but unless you are doing this simply for educational purposes, I wouldn't recommend it. The end result will be confusing and unmaintainable. I highly recommend renaming one or both of the modules instead of messing around with obscure Python import related features.