A mistake which almost went me mad

Ian Kelly ian.g.kelly at gmail.com
Mon Mar 7 11:19:15 EST 2016


On Thu, Mar 3, 2016 at 11:50 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> I think that relative imports should ameliorate this, as I usually
> hit it when I'm using smtplib which in turn imports "email" (and, in
> 2.x when it found my local email.py would crash and burn). If it used
> a relative import that forced it to find the one in the stdlib, it
> should(?) prevent it from finding my local version first.

Relative imports only work inside packages. You can't use a relative
import to import one top-level module from another.

Besides, the relative import doesn't help to disambiguate in this
case. The absolute path of the stdlib email module is "email". The
absolute path of the module in your CWD is also "email". Why should a
relative import prefer one over the other? So I would think that even
if it worked, it would still just end up importing the first one it
finds on your sys.path.



More information about the Python-list mailing list