relative import broken?

Alex Martelli aleax at mac.com
Wed May 2 00:02:24 EDT 2007


Alan Isaac <aisaac at american.edu> wrote:

> > "Alex Martelli" <aleax at mac.com> wrote in message
> > news:1hxeg2v.1ct59rv3oyq87N%aleax at mac.com...
> > > I don't know of any "pretty" way -- I'd do it by path manipulation
> > > (finding mypackage from os.path.abspath(__file__) and inserting its
> > > _parent_ directory in sys.path).
> >
> 
> "Alan Isaac" <aisaac at american.edu> wrote in message
> news:7nNZh.4420$st3.1414 at trnddc06...
> > Yes, that seems to be the standard solution.
> > I find it ugly.
> 
> Just to confirm that I am right to find it ugly:
> does this not clearly introduce the possibility of name clashes?
> Or am I overlooking some trick?

If you use sys.path.insert(0, ...), not sys.path.append, I'm not sure
what "name clashes" you're thinking of -- as long as you avoid naming
your modules the same as ones in the standard library (which is a good
practice I heartily recommend), of course, what scenario do you fear?

You can have more control about _where_ stuff can get imported from by
directly calling the __import__ builtin, but that's not often needed.


Alex



More information about the Python-list mailing list