Chris Angelico added the comment:

This still happens in current CPython, and doesn't even require a package:

rosuav@sikorsky:~$ echo 'from . import x' >syserr.py
rosuav@sikorsky:~$ python3 syserr.py
Traceback (most recent call last):
  File "syserr.py", line 1, in <module>
    from . import x
SystemError: Parent module '' not loaded, cannot perform relative import


This just caused some confusion for a student of mine who had migrated from 
Python 2. Converting intra-package imports from "import spam" to "from . import 
spam" made them work; converting a non-package import the same way caused this 
problem. ImportError would be massively preferable.

But I'm not understanding something here. If I set __package__ inside the 
module, the behaviour changes accordingly:

rosuav@sikorsky:~$ cat syserr.py 
__package__ = 'sys'
from . import version
print(version)
rosuav@sikorsky:~$ python3 syserr.py 
3.6.0a0 (default:ac94418299bd+, Jan 15 2016, 08:44:02) 
[GCC 4.9.2]


Leaving __package__ unset has it implicitly be None. Whether it's None or '', 
the package checks should simply not be made - see three lines above the line 
Brett linked to (in today's code, that's 
https://hg.python.org/cpython/file/ac94418299b/Lib/importlib/_bootstrap.py#l925 
- hasn't changed). So I'm not sure why the checks are even happening. However, 
that probably means more about my exploration and testing than it does about 
the code; editing the text of the message doesn't result in a change, so I'm 
obviously not updating the frozen version.

----------
nosy: +Rosuav
versions: +Python 3.5, Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18018>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to