importing module from two packages with the same name

Gordon McMillan gmcm at hypernet.com
Sun Oct 6 07:16:21 EDT 2002


David Garamond 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')

-- Gordon
http://www.mcmillan-inc.com/



More information about the Python-list mailing list