This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot load sub package having a 3-dot relative import
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, yon
Priority: normal Keywords:

Created on 2020-03-08 20:01 by yon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg363678 - (view) Author: Yon Ar Chall (yon) Date: 2020-03-08 20:01
Hi there !

I was trying to use importlib.util.spec_from_file_location() to import a nested package containing a 3-dot relative import.

$ tree ~/myproj
/home/yon/myproj
└── mypkg
    ├── __init__.py
    └── subpkg
        ├── __init__.py
        ├── subsubpkg_abs
        │   └── __init__.py
        └── subsubpkg_rel
            └── __init__.py

Relative import here :

$ cat ~/myproj/mypkg/subpkg/subsubpkg_rel/__init__.py 
from ... import subpkg

Absolute import here (for comparison purpose) :

$ cat ~/myproj/mypkg/subpkg/subsubpkg_abs/__init__.py 
import mypkg.subpkg 

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.util import spec_from_file_location
>>> spec = spec_from_file_location("subsubpkg_abs", "/home/yon/myproj/mypkg/subpkg/subsubpkg_abs/__init__.py")
>>> spec.loader.load_module()
<module 'subsubpkg_abs' from '/home/yon/myproj/mypkg/subpkg/subsubpkg_abs/__init__.py'>
>>> spec = spec_from_file_location("subsubpkg_rel", "/home/yon/myproj/mypkg/subpkg/subsubpkg_rel/__init__.py")
>>> spec.loader.load_module()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap_external>", line 388, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 809, in load_module
  File "<frozen importlib._bootstrap_external>", line 668, in load_module
  File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/yon/myproj/mypkg/subpkg/subsubpkg_rel/__init__.py", line 1, in <module>
    from ... import subpkg
ValueError: attempted relative import beyond top-level package
>>>

Raw import just works (as importlib.import_module() does) :

>>> import mypkg.subpkg.subsubpkg_rel
>>>
msg363682 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-08 20:39
In this line:

> spec = spec_from_file_location("subsubpkg_rel", "/home/yon/myproj/mypkg/subpkg/subsubpkg_rel/__init__.py")

try providing the fully-qualified name, instead of just "subsubpkg_rel". That is:

> spec = spec_from_file_location("mypkg.subpkg.subsubpkg_rel", "/Users/mdickinson/Desktop/test/mypkg/subpkg/subsubpkg_rel/__init__.py")
History
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 84086
2020-03-09 09:51:22mark.dickinsonsetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-03-08 20:39:17mark.dickinsonsetnosy: + mark.dickinson
messages: + msg363682
2020-03-08 20:01:08yoncreate