importing module from two packages with the same name

David Garamond davegaramond at icqmail.com
Mon Oct 7 05:10:19 EDT 2002


Gordon McMillan wrote:
>>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)
>>
>>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')
> 
> You can have only one Package1, but what that means is
> you can have only one Package1/__init__.py. You can
> extend the search of Package1 to cover more than one
> directory by appending the extra directories to Package1's
> __path__ attribute.
> 
> In __init__.py:
> __path__.append('/home/david/proj/Proj1/lib/python/Package1')

btw, i just found out about __builtin__.__import__ and the ihooks module 
today. cool, you can do even weird things with these stuffs. it seems 
that guido had thought about this thing much earlier on. thanks, guido :-)

-- 
dave





More information about the Python-list mailing list