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

Huaiyu Zhu hzhu at knowledgetrack.com
Wed Jun 28 19:56:19 EDT 2000


On 28 Jun 2000 22:51:18 GMT, Gordon McMillan <gmcm at hypernet.com> wrote:
>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.

Oops!  You are right.  That's of course what I meant.

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

I don't follow you here.  Both AAA and BBB are packages.   In fact they are

python/
   AAA/
      __init__.py
   BBB/
      __init__.py
      aaa.py

Withing python/BBB/aaa.py there is an 'import AAA' which imports from
python/AAA/__init__.py on Unix but from python/BBB/aaa.py on Windows.  Had
the file name been python/BBB/AAA.py then any module within BBB cannot
import the global package AAA, on any platform.

My question is exactly how to make imports absolute instead of relative.

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

Hmm, what is useful?  Having '.' in path or allowing packages to demand
different content of path?  Yes I'd agree to the former (but it's biting me
now) but I don't see the latter.

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

Where can I find it?  www.python.org does not respond right now.  On
starship I got this (probably very old) stuff

   Greg's Python Stuff

   Sorry... but I don't have any interesting tidbits here right now. I'll get
   these pages updated Real Soon Now. :-) 

   (let's say by the 1998 Python Conference...) 


Huaiyu

-- 
Huaiyu Zhu                       hzhu at users.sourceforge.net
Matrix for Python Project        http://MatPy.sourceforge.net 



More information about the Python-list mailing list