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.

Incompatibility with newest "importlib_resources" backport of "importlib.resources"

See original GitHub issue

Description

First off, thank you for all your hard work 😃

I have found that a --onefile / --standalone application using the package jsonschema no longer works, since jsonschema v4.x.x.

I have narrowed it down to the jsonschema._utils.load_schema function, which appears to have changed from using pkgutil to importlib.resources / importlib_resources in its v3 -> v4 update. This seems to have broken its functionality with Nuitka.

In jsonschema 3.2.0 (working), the load_schema function used pkgutil

def load_schema(name):
    """
    Load a schema from ./schemas/``name``.json and return it.
    """

    data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
    return json.loads(data.decode("utf-8"))

In jsonschema 4.2.1 (no longer working), it appears the load_schema function now uses importlib_resources

if sys.version_info >= (3, 9):  # pragma: no cover
    import importlib.resources as resources
else:  # pragma: no cover
    import importlib_resources as resources

...

def load_schema(name):
    """
    Load a schema from ./schemas/``name``.json and return it.
    """

    path = resources.files(__package__).joinpath(f"schemas/{name}.json")
    data = path.read_text(encoding="utf-8")
    return json.loads(data)

Error Logs

$ ./example

Traceback (most recent call last):
  File "/tmp/.mount_examplWYktsX/example.py", line 1, in <module>
    import jsonschema
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/tmp/.mount_examplWYktsX/jsonschema/__init__.py", line 29, in <module jsonschema>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/tmp/.mount_examplWYktsX/jsonschema/validators.py", line 349, in <module jsonschema.validators>
  File "/tmp/.mount_examplWYktsX/jsonschema/_utils.py", line 61, in load_schema
  File "/tmp/.mount_examplWYktsX/importlib_resources/abc.py", line 73, in read_text
  File "/tmp/.mount_examplWYktsX/importlib_resources/_adapters.py", line 141, in open
FileNotFoundError: Can't open orphan path

Nuitka Version:

Tried on both latest version, and development branch version.

  • v0.6.17.6
  • develop branch (v0.6.18rc7)

Operating System

  • Ubuntu 20.04
  • x86_64
  • Linux 5.8.0-59-generic

Installation Method:

  • pip (poetry with virtual environment and pyproject.toml

Example

example.py

import jsonschema
print(jsonschema)

pyproject.toml

[tool.poetry]
name = "example"
version = "0.1.0"
description = ""
authors = ["..."]

[tool.poetry.dependencies]
python = "^3.8"
jsonschema = "^4.2.1"
Nuitka = "^0.6.17"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

nuitka.sh

#!/bin/bash
python3 -m nuitka  \
    example.py     \
    -o example     \
    --onefile

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kayhayencommented, Nov 16, 2021

Part of the hotfix indeed. Thanks for your report.

1reaction
kayhayencommented, Nov 15, 2021

It seems importlib_resources is supposed to be working as of 0.6.1 which is a long time away, but maybe it is the one that changed. And I also couldn’t reproduce this with 3.9 and only with 3.8, which is using that backport.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deprecating importlib.resources legacy API - Core Development
In 2018, users reported a substantial deficiency in importlib.resources API that prevented a common use-case previously supported by pkgutil ...
Read more >
History - importlib-resources 5.10.2.dev4+gf8c6b66 ...
Fixes issue where backport compatibility module was masking this fallback behavior only to discover the defect when applying changes to CPython. v5.1.0#. 18...
Read more >
importlib_resources backport not compatible with pyfakefs?
I'm using the importlib_resources backport for python 3.6 compatibility. The code works for pythons 3.6-3.8. However, the pytest tests, using ...
Read more >
importlib-resources - PyPI
importlib_resources is a backport of Python standard library importlib.resources module for older Pythons. The key goal of this module is to replace parts ......
Read more >
Loading Resource Files — PyOxidizer 0.23.0 documentation
Maintaining Compatibility With Python <3.7¶ ; importlib.resources to older Python versions. Essentially, you can always get the APIs from the latest Python ...
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