Ben Lewis <[email protected]> added the comment:
>>> foo = 'oops'
>>> from . import foo as fubar # should raise ImportError
>>> fubar
'oops'
After further investigation, the problem is that builtins.__import__ (the c
port version) does not replicate the behaviour of importlib.__import__ (the
python reference version):
>>> import builtins, importlib
>>> __package__ is None
True
>>> importlib.__import__('', globals(), locals(), ('foo',), 1)
ImportError
>>> builtins.__import__('', globals(), locals(), ('foo',), 1)
<module '__main__' (built-in)>
A further discrepancy is that for deeper relative imports, builtins.__import__
raises a ValueError instead of ImportError (contrary to expectation/spec):
>>> from ...... import foo
ValueError
A simple work around uses the python implementation to restore expected
behaviour:
>>> builtins.__import__ = importlib.__import__
>>> from ...... import foo
ImportError
>>> from curses import ascii
>>> from . import ascii
ImportError
PS: Brett Cannon, to replicate please copy and paste lines in correct order :-)
----------
resolution: rejected ->
status: closed -> open
title: relative import_from without parent -> relative import without parent
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37409>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com