(Serious?) package namespace problem (and a proposal)

Bernhard Herzog herzog at online.de
Thu Jun 29 07:55:36 EDT 2000


hzhu at knowledgetrack.com (Huaiyu Zhu) writes:

> Here's a retake:
> 
> $PYTHONPATH/
>    AAA/
>       __init__.py
>    BBB/
>       __init__.py
>       aaa.py
>       ccc.py
>    CCC/
>       ...
> 
> Within BBB/aaa.py the statement 'import AAA' imports from AAA/__init__.py on
> Unix but from BBB/aaa.py on Windows.
> 
> The reason is that within the BBB package python first looks for a module
> called AAA within the package (which it found as aaa.py on Windows) before
> it looks for a package named AAA.

Exactly. Since BBB was apparently written with importing AAA in mind,
simply don't name the modules in BBB so that they hide AAA. 

This is very similar to the way local variables hide global variables.

> Although this is often the desired behavior, the other way around is also
> fairly common.  In my case AAA and CCC are packages written by others and
> BBB is a wraper pacakge that imports the other packages to do the real work
> but provides different interfaces for convenience.  Using identical names
> seems very natural here.

Well, you could do something like this:

    BBB/
       __init__.py
       myaaa.py
       myccc.py

and have a BBB/__init__.py like this:

import myaaa
AAA = myaaa
del myaaa

import myccc
CCC = myccc
del myaaa

> Therefore I think it is desirable for python to provide a mechanism for
> programmers to describe absolute path of imports.  Like on Unix, writing
> /bin/sh in stead of sh in a script will guarantee its behavior no matter
> where it is run and what the current PATH the user has.

I don't think you need to be able to use absolute paths. What would that
mean exactly anyway? What would indeed be useful would be to explicitly
refer to a module in the global module name sapce, i.e. one directly in
sys.path.

> My previous proposal was to use
> import pythonroot.AAA

'import global.AAA' or 'from global import AAA' would look better, I
think.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list