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.

Author yon
Recipients yon
Date 2020-03-08.20:01:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1583697668.13.0.326902035293.issue39905@roundup.psfhosted.org>
In-reply-to
Content
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
>>>
History
Date User Action Args
2020-03-08 20:01:08yonsetrecipients: + yon
2020-03-08 20:01:08yonsetmessageid: <1583697668.13.0.326902035293.issue39905@roundup.psfhosted.org>
2020-03-08 20:01:08yonlinkissue39905 messages
2020-03-08 20:01:07yoncreate