(Serious?) package namespace problem (and a proposal)
Gordon McMillan
gmcm at hypernet.com
Wed Jun 28 18:51:18 EDT 2000
Huaiyu Zhu wrote:
>Given the following directory structure,
>
>python/
> AAA.py
> BBB/
> __init__.py
> AAA.py
>
>if sys.path contains 'python', then
>
>from AAA import *
>from BBB.AAA import *
>
>would do the right thing at most locations, except when pwd is python/
>and when '.' is before 'python' in sys.path, in which case they both
>import from python/AAA.py.
>Is this a fair description of current situation?
There's a problem (actually a couple) but this isn't the right description.
If you're in python/BBB then "import AAA" and "import BBB.AAA" both get
BBB.AAA.
>...If so this makes
>package name spaces essentially flat. That is, in order to avoid name
>clashes one has to avoid identical module names even if they are buried
>deep inside packages.
Yes, (except I wouldn't call that "flat" - I would interpret "flat" as no
subpackages).
>This is even worse on Windows - its file names
>are caseless - as one has to avoid filenames with different cases as
>well.
Too true...
>This situation occurs quite often in practice. For example, AAA could
>be a module for doing something. BBB could be a wrapper package for
>doing things somewhat differently. So within BBB/AAA.py one calls
>'import AAA' to get the real job done, and oops, it imports itself!
Besides avoiding using the same name, you could make the top level a
package, then python.BBB.AAA could import python.AAA and it would be
completely unambiguous. Which is a good reason to avoid relative imports -
they are by nature ambiguous.
>Although it is not advisable to put '.' in path, especially at front, a
>lot of people actually do. And the functionality of a package should not
>depend on this detail. Otherwise what happens if one imports several
>packages each expecting a different ordering of sys.path?
Actually it is very useful, since it allows you to have a script have some
private support modules that don't clutter up sys.path for other scripts.
>So now to my proposed solution. Is it possible to have something like
>this?
>
>sys.rootpath = ['/usr/lib/python1.5',
>'/usr/lib/python1.5/site-packages'] from rootpath.AAA import *
>from rootpath.BBB.AAA.import *
You could use Greg Stein's imputils, but in this case just packagizing the
whole thing is a more straightforward solution.
- Gordon
More information about the Python-list
mailing list