PyWart: "Python's import statement and the history of external dependencies"
ian.g.kelly at gmail.com
Fri Nov 21 18:42:44 CET 2014
On Fri, Nov 21, 2014 at 9:12 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> The only time I've been stung by name overloading is in the indirect
> case of creating a local email.py file and then importing smtplib
> only to have things break in unforeseen ways. If smtplib used
> relative imports for $STDLIB/email.py I suspect it would ameliorate
> that particular issue for me.
Relative imports are based on package namespaces and can only be done
within a package. There's no way to do a relative import from a
separate top-level package, whether they happen to be found in the
same directory or not.
$ mkdir p1
$ touch p1/__init__.py
$ echo 'from ..p2 import m2' > p1/m1.py
$ mkdir p2
$ touch p2/__init__.py
$ touch p2/m2.py
$ python3 -c 'import p1.m1'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/ikelly/p1/m1.py", line 1, in <module>
from ..p2 import m2
ValueError: attempted relative import beyond top-level package
More information about the Python-list