import in Python3.3

rocky rocky at gnu.org
Tue Mar 26 11:37:00 EDT 2013


On Tuesday, March 26, 2013 3:04:44 AM UTC-4, Terry Reedy wrote:
> On 3/24/2013 7:12 PM, Fabian von Romberg wrote:
> 
> > Hi,
> 
> >
> 
> > I have a package name collections and inside of my package I want to
> 
> > import the collections package from the standard library, but there
> 
> > is name conflicts.
> 
> 
> 
> Yes. I strongly advise against trying to do this.
> 
> 
> 
> > How do I import explicitly from the standard library?
> 
> 
> 
> Manipulate sys.path. 

This is a common suggestion for working *around* the issue, but it has problems of its own.

In my opinion using sys.path is clunky - you may want to save an restore the value around use especially if the the place you are using it is not at the top level but in some inner less-exposed sub module. And make sure to restore sys.path in the presence of exceptions getting thrown somewhere inside the import. 

It is also a bit of a security nightmare. If something evil gets injected in there what does it effect and how would you know it? sys.path provides a series of directories where and evil masquerading Python program can linger.

And again, I get the impression that for the use case asked about, there isn't much ambiguity. If I am in mypackage.foo and I want to access mypackage.collections I should be able to say something like that without ambiguity or that much inference or directory searching. If mypackage.colletions is not found inside the same directory as mypackage.foo, often I DON'T WANT Python to helpfully go searching around other places for it which sys.path will do. Instead what I probably want is Python to give me an error. 

So again I come to import_relative, http://code.google.com/p/pyimport-relative/.  And again, I wish this package didn't have to exist.

> If you don't know how, either read about sys.path 
> 
> (see index) or don't do it.
> 
> 
> 
> -- 
> 
> Terry Jan Reedy




More information about the Python-list mailing list