Author Rosuav
Recipients Arfrever, Rosuav, brett.cannon, eric.snow, flox, ncoghlan, pitrou
Date 2016-01-14.21:55:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1452808511.84.0.919651604686.issue18018@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-01-14 21:55:11Rosuavsetrecipients: + Rosuav, brett.cannon, ncoghlan, pitrou, Arfrever, flox, eric.snow
2016-01-14 21:55:11Rosuavsetmessageid: <1452808511.84.0.919651604686.issue18018@psf.upfronthosting.co.za>
2016-01-14 21:55:11Rosuavlinkissue18018 messages
2016-01-14 21:55:11Rosuavcreate