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

Sean Blakey sblakey at freei.com
Wed Jun 28 18:16:11 EDT 2000


On Wed, Jun 28, 2000 at 09:56:27PM +0000, Huaiyu Zhu wrote:
> 
> On Wed, 28 Jun 2000 14:57:41 -0600, Bjorn Pettersen <bjorn at roguewave.com> wrote:
> >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...
> >
> 
> Noop.  The issue is not what names are imported from a package, but from
> which package/module files they are imported.
> 
> Perhaps I shouldn't have "abstracted" my example so much as to sidetrack the
> reader's minds.  In fact I was doing
> 
> from AAA import aaa
> from BBB.AAA import aaa
> 
> but the first statement does the wrong thing in the directory python/BBB
> because it imports from the file python/BBB/AAA.py instead of the intended
> file python/AAA.py.
> 
> Huaiyu
> 

OK, I think I see what you mean.  You are objecting to the way that python
searches first for modules in the local directory before searching the normal
PYTHONPATH, correct?

For example, If you have a script string.py and foo.py in your local directory
and foo has an "import string" statement, you want foo to import string from the
standard library instead of from your string.py script.

It seems to me that you are expecting python too read your mind about where you
want the modules imported from.  I believe python's behavior makes sens for most
cases.

If you don't want python to search the current directory for imports, there is a
very simple solution: put this snippet above all of your other imports:
import sys
sys.path.remove('.')

Note that this won't work if you have a script named sys.py in your current
directory.
    -Sean

-- 
Sean Blakey, sblakey at freei.com
Software Developer, FreeInternet.com
(253)796-6500x1025
Disraeli was pretty close: actually, there are Lies, Damn lies, Statistics,
Benchmarks, and Delivery dates.




More information about the Python-list mailing list