(Serious?) package namespace problem (and a proposal)
Bjorn Pettersen
bjorn at roguewave.com
Wed Jun 28 16:57:41 EDT 2000
Note that all the problems you describe completely go away if you do:
import AAA
import BBB.AAA
IMHO you deserve any/all complications you get when you do "from foo
import *", and I strongly believe that we shouldn't do _anything_ to
encourage this use...
-- bjorn
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? 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. This is even worse on Windows - its file names are caseless - as
> one has to avoid filenames with different cases as well.
>
> 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!
>
> 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?
>
> In Unix shells, however, this can always be overcome by using absolute
> paths, like /the/absolute/path/command, whatever the PATH is.
>
> 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 *
>
> This guarantees that module names within packages are well separated. One
> can still use
> from AAA import *
> to import from any module named AAA that happens to be in the path. IMHO,
> this would not break any existing code (unless it happens to contain the new
> keyword 'rootpath'). It would certainly facilitate much easier use of
> packages. The only real problem I can see of is that developers might (for
> what reason?) rush to occupy real estates under rootpath. But since the
> standard distribution is centrally controlled and add-ons are user
> controlled, this shouldn't be a big problem.
>
> --
> Huaiyu Zhu hzhu at users.sourceforge.net
> Matrix for Python Project http://MatPy.sourceforge.net
> --
> http://www.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list