package search

imho certo at comeno.it
Sun Jun 11 08:53:08 EDT 2006


boris ha scritto:
> I have two directories, lib1 and lib2, that both contain the package
> foo, one with the submodule mod1
> and the other with the submodule mod2:
> [...]
> Now this script:
> 
> import sys
> sys.path.append("lib1")
> sys.path.append("lib2")
> import foo.mod1
> 
> will find the module foo.mod1, while the same script with the two
> append-lines interchanged will not:
> 
> import sys
> sys.path.append("lib2")
> sys.path.append("lib1")
> import foo.mod1
> 
> The error is:
> 
>     import foo.mod1
> ImportError: No module named mod1
 > [...]

You just have to put in "__init__.py" in "lib2" (the package directory 
you are "extending"), the following lines:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

"__path__", in each __init__ module, is a list initialized with the 
module's path, but you can extend it by appending paths where you want 
the interpreter to look for further modules.
pkgutil.extend_path automatically appends to __path__ all subdirectories 
of directories on sys.path named after the package.

HTH :-)

Diego.



More information about the Python-list mailing list