changing sys.path

Andrea Crotti andrea.crotti.0 at gmail.com
Fri Feb 10 10:38:18 EST 2012


On 02/10/2012 03:27 PM, Peter Otten wrote:
> The package a will be either a.c/a/ or a.b/a/ depending on whether 
> a.c/ or a.b/ appears first in sys.path. If it's a.c/a, that does not 
> contain a c submodule or subpackage. 


I would agree if I didn't have this declaration
__import__('pkg_resources').declare_namespace(__name__)
in each subdirectory.

And how do you explain the fact that changing the order everything works?
Namespace packages are supposed to work exactly like this, if it doesn't 
resolve the
"c" instead of raising an Exception it goes forward in the sys.path and 
try again, which
is what actually happens when I do this

sys.path.append(path.abspath('ab'))
sys.path.append(path.abspath('ac'))

from a.b import api as api_ab
from a.c import api as api_ac

Maybe this:
Definition: pkgutil.extend_path(path, name)
Docstring:
Extend a package's path.

Intended use is to place the following code in a package's __init__.py:

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


might come handy, from what I'm gathering is the only way to have a more 
dynamic path
manipulation with namespace packages..



More information about the Python-list mailing list