PEP 328, absolute/relative import (was: Re: Bizarre error from help())

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Aug 10 20:43:30 EDT 2005


Peter Hansen <peter at engcorp.com> wrote:
> Ben Finney wrote:
> > Roy Smith <roy at panix.com> wrote:
> >>[current-directory module shadowing a system module]
> > 
> > All hail the coming of PEP 328:
> >     <URL:http://www.python.org/peps/pep-0328.html>
> 
> Which, unless I misunderstand, would do nothing to change the
> behaviour of the OP's situation unless the mistakingly named
> "string.py" module was actually inside a package.  If it was just in
> the current directory at the time, I don't think the PEP328 would
> have affected the situation.

Once PEP 328 is fully implemented, all bare 'import foo' statements
specify absolute imports (i.e. from sys.path only). To perform a
relative import (e.g. from current directory) will require different
syntax.

    "[...] relative imports will use leading dots. A single leading
    dot indicates a relative import, starting with the current
    package. Two or more leading dots give a relative import to the
    parent(s) of the current package [...]"

    <URL:http://www.python.org/peps/pep-0328.html#guido-s-decision>

So, under PEP 328 rules, the original poster's current-directory
module could only be imported (a) if the current directory was in
sys.path, or (b) if the code specified a relative import. The
accidental shadowing of the stdlib module could not happen.

-- 
 \     "You know what would make a good story? Something about a clown |
  `\    who makes people happy, but inside he's real sad. Also, he has |
_o__)                                severe diarrhea."  -- Jack Handey |
Ben Finney <http://www.benfinney.id.au/>



More information about the Python-list mailing list