[Python-Dev] Comment regarding PEP 328
Guido van Rossum
gvanrossum at gmail.com
Thu Feb 24 04:00:02 CET 2005
> In a recent discussion in a SF patch, I noticed that PEP 328* only seems
> to support relative imports within packages, while bare import
> statements use the entirety of sys.path, not solving the shadowing of
> standard library module names.
Hm. I'm not convinced that there is a *problem* with shadowing of
standard library module names. You shouldn't pick a module name that
shadows a standard library module, or if you do you shouldn't want to
be able to still use those modules that you're shadowing. Anything
else is just asking for trouble.
> I have certainly forgotten bits of discussion from last spring, but I
> would offer that Python could offer standard library shadowing
> protection through the use of an extended PEP 328 semantic.
> More specifically; after a 'from __future__ import absolute_import'
> statement, any import in the module performing "import foo" will only
> check for foo in the standard library, and the use of the leading period,
> "from . import foo", the will signify relative to the current path. **
And how exactly do you define "the standard library"? Anything that's
on sys.path? That would seem the only reasonable interpretation to me.
So I take it that you want the "script directory" off that path.
(Let's for the sake of argument call it ".".)
> The lack of a 'from __future__ import absolute_import' statement in a
> module will not change the import semantic of that module.
It's hard to imagine how this would work. sys.path is global, so
either "." is on it, or it isn't. So things in "." are either
considered part of the standard library, or they are not; this can't
be made dependent on the module's importation of something from
> This allows current code to continue to work, and for those who want to
> choose names which shadow the standard library modules, a way of dealing
> with their choices.
My suggested way of dealing with their choices is summarized in the
first paragraph of my reply above.
> Further, in the case of PEP 328, the package relative imports were to
> become the default in 2.6 (with deprecation in 2.5, availability in 2.4),
> but with the lack of an implementation, perhaps those numbers should be
> incremented. If the behavior I describe is desireable, it would subsume
> PEP 328, and perhaps should also become the default behavior at some
> point in time (perhaps in the same adjusted timeline as PEP 328).
That's a separate issue; the absolute/relative import part of PEP 328
didn't make it into 2.4 so I suppose we should ++ all those version
> Alternatively, PEP 328 could be implemented as-is, and a second future
> import could be defined which offers this functionality, being
> permanently optional (or on a different timeline) via the future import.
I don't like permanently optional language features; that causes too
much confusion. I'd much rather settle on clear semantics that
everyone can understand (even if they may disagree).
But I certainly would prefer that the proposed feature becomes a
separate PEP which can be discussed, accepted or rejected, and
implemented separately from PEP 328, which is complete and accepted
and just awaiting someone to implement it.
> Essentially, it would ignore the empty path "" in sys.path when the
> functionality has been enabled via the proper future import in the
> current module.
But it's not always "" -- it's the directory where the "main" script was found.
Let me explain the biggest problem I see for your proposal: what would
be the canonical name for a module imported using your "new relative
semantics"? Remember, the canonical name of a module is its __name__
attribute, and the key that finds it in the sys.modules dict. Because
there's only one sys.modules dict (barring restricted execution
sandboxes), the canonical name must be unique. So if there's a
standard library module string, its canonical name is "string". Now
suppose you have your own non-standard-linrary module read from a file
string.py. What should its canonical name be? It can't be "string"
because that's already reserved for the standard library module name.
The best solution I can think of for this off the top of my head is to
somehow allow for the arrangement of a pseudo-package named __main__
and to make all these non-standard-library modules reside (logically)
in that module. If you can write a PEP along those lines you may be on
to something -- but I expect that the way to turn it on is not to
import something from __future__ but perhaps from __main__. I'm not
exactly sure how to get "." off sys.path, but maybe you can think
about that for your PEP proposal. What do you say?
> - Josiah
> * PEP 328 first describes the use of parenthesis in import statements so
> that long import listings do not require backslash-escaping of newlines.
> It then describes a semantic for not checking sys.path when performing
> an import, as well as allowing parent, cousin, uncle, etc., imports via
> additional leading periods. "from . import foo" for sibling imports,
> "from .. import foo" for parent imports, etc.
> ** I describe the semantic as being a per-module option, as this is the
> only backwards-compatible mechanism in the near future (Python 2.5). An
> import implementation would merely check the existance of the proper
> name binding -> object pair in the importer's global namespace. The
> standard library would need to be modified if or when current behavior
> is deprecated (this would extend the modules needing to be modified due
> to PEP 328).
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev