importing module from two packages with the same name

Aldo Cortesi aldo at nullcube.com
Sun Oct 6 05:36:32 EDT 2002


Thus spake David Garamond (davegaramond at icqmail.com):

> i have two modules in different location, but they belong to the same 
> package name:
> 
>  /home/david/usr/lib/python/Package1/Libra.pm  (1)
                                             ^^
>  /home/david/proj/Proj1/lib/python/Package1/Scorpio.pm   (2)
                                                      ^^
Meant to be .py, right? :)

> i have added both paths to sys.path:
> 
>  import sys
>  sys.path.insert(0, '/home/david/usr/lib/python')
>  sys.path.insert(0, '/home/david/proj/Proj1/lib/python')
> 
> once python found Package1 in (1):
> 
>  import Package1.Libra
> 
> it won't search in (2), so i can't import Package1.Scorpio. any 
> hint/trick to work around this? is this the intended python module 
> search behaviour?

Certainly this behaviour is intended - Python finds the
first occurence of Package1 in the path, and initialises it.
Subsequent checks for module contents are then done on the
already-initialised Package1.

The first recommendation here is, of course, is to eliminate
your problem by refactoring your packages. If this is
impossible, however, you may be able to do something like:

sys.path.insert(0, '/home/david/usr/lib/python')
import Package1.Libra
reload("Package1")
sys.path.insert(0, '/home/david/proj/Proj1/lib/python')
import Package1.Scorpio

But don't do this if you can help it.




Aldo



-- 
Aldo Cortesi
aldo at nullcube.com
http://www.nullcube.com




More information about the Python-list mailing list