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

Bernhard Herzog herzog at online.de
Wed Jun 28 19:56:19 EDT 2000


hzhu at knowledgetrack.com (Huaiyu Zhu) writes:

> 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.

If your cwd is python then the sys.path entry for python/ and '.' are
the same directory. Why would that lead to problems? The only thing I
can think of is that other directories in sys.path also have AAA
modules.

> 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!

That's the way packages are designed in python, regardless of your cwd.
'import xyzzy' in a module in a package will look for a module 'xyzzy'
in the same package first and if not found there will look in the
directories in sys.path.


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



More information about the Python-list mailing list