classification
Title: SystemError when importing from a non-package directory
Type: behavior Stage: test needed
Components: Interpreter Core Versions: Python 3.6, Python 3.5, Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: SystemError: Parent module '' not loaded, cannot perform relative import
View: 18018
Assigned To: Nosy List: Rosuav, brett.cannon, eric.snow, fossilet
Priority: normal Keywords:

Created on 2015-07-30 06:46 by fossilet, last changed 2016-01-14 22:10 by brett.cannon. This issue is now closed.

Messages (4)
msg247653 - (view) Author: Yongzhi Pan (fossilet) * Date: 2015-07-30 06:46
In an empty dir, e.g. Desktop, the following import raises SystemError:

>>> from . import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: Parent module '' not loaded, cannot perform relative import

The documentation says that SystemError should be reported as a bug.
msg247678 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-07-30 15:00
You are correct that SystemError indicates a non-fatal issue in the interpreter and thanks for reporting the matter.  In this case I'd say that SystemError is the wrong exception type.  I expect that ImportError (or perhaps RuntimeError) is more appropriate.  However, Brett will have a better idea of why we raise SystemError instead.
msg247687 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-07-30 17:00
It's that way because that's how import.c has it coded in Python 2.7: https://hg.python.org/cpython/file/309cbbc32491/Python/import.c#l2462 . Here is how it looks in Python 2:

%%python2
from __future__ import absolute_import
from . import foo
-----------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: Attempted relative import in non-package


I think another sanity check needs to be added to https://github.com/python/cpython/blob/829b49cbd2e4b1d573470da79ca844b730120f3d/Lib/importlib/_bootstrap.py#L919 for when __package__ is None but level > 0 and then raise the ValueError like in Python 2.
msg258231 - (view) Author: Chris Angelico (Rosuav) * Date: 2016-01-14 22:01
Linking this issue with #18018 - same problem, same situation.
History
Date User Action Args
2016-01-14 22:10:15brett.cannonsetstatus: open -> closed
resolution: duplicate
2016-01-14 22:10:00brett.cannonsetsuperseder: SystemError: Parent module '' not loaded, cannot perform relative import
2016-01-14 22:01:00Rosuavsetnosy: + Rosuav
messages: + msg258231
2015-07-30 17:00:14brett.cannonsetmessages: + msg247687
stage: needs patch -> test needed
2015-07-30 15:00:41eric.snowsettype: behavior
stage: needs patch
messages: + msg247678
versions: + Python 3.5, Python 3.6
2015-07-30 12:24:59r.david.murraysetnosy: + brett.cannon, eric.snow
2015-07-30 06:46:07fossiletcreate