question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lack of support for importlib_resources

See original GitHub issue

Hey,

While evaluating nuitka for a somewhat larger problem of mine, I hit a problem due to using importlib_resources. The problem seems to be that nuitka doesn’t set module.__spec__.module_search_locations for packages to a list or string as requested by PEP451.

Consider the minimal example bar.py:

# bar.py
import importlib_resources
% python -m nuitka --follow-imports bar.py            
Nuitka:WARNING:/home/thomas/temp/gna/lib/python3.5/site-packages/importlib_resources/_py2.py:9: Cannot find 'pathlib2' in package 'importlib_resources' as absolute import (tried pathlib2).

% ./bar.bin                                            
ModuleSpec(name='importlib_resources', loader=<class '_nuitka_compiled_modules_loader'>)
Traceback (most recent call last):
  File "/home/thomas/temp/bar.py", line 1, in <module>
    import importlib_resources
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "/home/thomas/temp/gna/lib/python3.5/site-packages/importlib_resources/__init__.py", line 36, in <module importlib_resources>
    __version__ = read_text('importlib_resources', 'version.txt').strip()
  File "/home/thomas/temp/gna/lib/python3.5/site-packages/importlib_resources/_py3.py", line 158, in read_text
    package = _get_package(package)
  File "/home/thomas/temp/gna/lib/python3.5/site-packages/importlib_resources/_py3.py", line 41, in _get_package
    raise TypeError('{!r} is not a package'.format(package))
TypeError: 'importlib_resources' is not a package

Checking the corresponding line, this error is raised due to module.__spec__.module_search_locations is None.

The problem is not restricted to importlib_resources but also appears when loading resources via importlib.resources on newer python. Grepping for module_search_locations, I can see that notably six assigns this as empty list in some locations, and refers to PEP451 which in turn says that this attribute must be set to a list or string whenever the object is a package.

I tried this in a virtualenv with both py3.5 and py3.7 with the stable version 0.6.0.6 from PyPI as well as 0.6.1rc9 from your download page:

% python -m nuitka --version 
0.6.0.6
Python: 3.5.5 (default, Sep  6 2018, 12:31:21) 
Executable: /home/thomas/temp/gna/bin/python
OS: Linux
Arch: x86_64

Best, Thomas

PS: thanks for all your work on this wonderful project:)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:20 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
coldfixcommented, Jan 2, 2019

Great! It now works like a charm!

1reaction
kayhayencommented, Dec 11, 2018

Seems this should be easy, I will look into this and see if I can include it for 0.6.1 still.

Nuitka starting having these __spec__ values a while ago, but apparently doesn’t create them fully compatible. Seems it would be enough to assign the __path__ value to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue 42531: importlib.resources.path() raises TypeError for ...
That path() fails in practice to support some packages is therefore a bug not a the absence of a feature. Regardless of whether...
Read more >
Unable to retrieve resources from a namespace package (#68)
It seems to me (without looking at the code) it should be straightforward to support loading resources from namespace packages, following the ...
Read more >
Why does `importlib.resources` require `__init__.py`?
When writing a python package that includes resources such as sample data files, why is it necessary to have an __init__.py in the ......
Read more >
importlib-resources 5.10.2.dev4+gf8c6b66.d20221216 ...
Resources can live on the file system or in a zip file, with support for other loader classes that implement the appropriate API...
Read more >
venv/Lib/site-packages/pip/_vendor/certifi/core.py - GitLab
from importlib.resources import path as get_path, read_text ... This fallback will work for Python versions prior to 3.7 that lack the.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found