Loading modules from several directories

Greg Fortune lists at gregfortune.com
Wed Sep 18 15:06:56 EDT 2002


If it is indeed a package, you don't need to append both paths.  Just use:

sys.path.append('/somedir/foo')
import foo.foostuff
import foo.project_a.projectstuff

Furthermore, any module in project_a can access modules in foo by using
import foo.foostuff and the package will know how to resolve the location.


Greg Fortune


Miki Tebeka wrote:

> Hello Eugene,
>> Trying to reuse the code I found a problem (with Python or me) where
>> if I have for example, a foo package in two separate directories
>> (where foo/ contains common files and foo/project_a/ contains
>> project-specific files) I can't load from both.
>> 
>> If I put the common path first in the sys.path it tries to load the
>> project-specific stuff from there too. If I try putting the
>> project-specific one first it can't find the common stuff.
>> 
>> Anyone know of a good solution for this problem?
> Doesn't
> 
> sys.path.append('/somedir/foo')
> sys.path.append('/somedir/foo/project_a')
> import foostuff
> import projectstuff
> 
> works?
> 
> Miki




More information about the Python-list mailing list