Skip to content

Python 3.9 importlib.resources fails in tests #7014

Closed
@bnavigator

Description

@bnavigator

Description

Running the pytest unittest for notebook 7 fails for me at test setup:

_________________________________________________________ ERROR at setup of test_notebook_handler __________________________________________________________

jp_root_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/root_dir')
jp_template_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/templates')
app_settings_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/app_settings')
user_settings_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/user_settings')
schemas_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/schemas')
workspaces_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/workspaces')
labextensions_dir = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_notebook_handler0/labextensions_dir')

    @pytest.fixture
    def make_notebook_app(  # noqa PLR0913
        jp_root_dir,
        jp_template_dir,
        app_settings_dir,
        user_settings_dir,
        schemas_dir,
        workspaces_dir,
        labextensions_dir,
    ):
        def _make_notebook_app(**kwargs):
            return JupyterNotebookApp(
                static_dir=str(jp_root_dir),
                templates_dir=str(jp_template_dir),
                app_url="/",
                app_settings_dir=str(app_settings_dir),
                user_settings_dir=str(user_settings_dir),
                schemas_dir=str(schemas_dir),
                workspaces_dir=str(workspaces_dir),
                extra_labextensions_path=[str(labextensions_dir)],
            )
    
        # Copy the template files.
>       for html_path in glob.glob(str(files("notebook.templates").joinpath("*.html"))):

tests/conftest.py:57: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib64/python3.9/importlib/resources.py:147: in files
    return _common.from_package(_get_package(package))
/usr/lib64/python3.9/importlib/_common.py:14: in from_package
    return fallback_resources(package.__spec__)
/usr/lib64/python3.9/importlib/_common.py:18: in fallback_resources
    package_directory = pathlib.Path(spec.origin).parent
/usr/lib64/python3.9/pathlib.py:1082: in __new__
    self = cls._from_parts(args, init=False)
/usr/lib64/python3.9/pathlib.py:707: in _from_parts
    drv, root, parts = self._parse_args(args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'pathlib.PosixPath'>, args = (None,)

    @classmethod
    def _parse_args(cls, args):
        # This is useful when you don't want to create an instance, just
        # canonicalize some constructor arguments.
        parts = []
        for a in args:
            if isinstance(a, PurePath):
                parts += a._parts
            else:
>               a = os.fspath(a)
E               TypeError: expected str, bytes or os.PathLike object, not NoneType

/usr/lib64/python3.9/pathlib.py:691: TypeError

The reason ist that the files importlib.resources in Python 3.9 cannot deal with the templates folder. It needs the importlib_resources backport:

abuild@skylab:~/rpmbuild/BUILD/notebook-7.0.2> python3.9
Python 3.9.17 (main, Jun 28 2023, 19:17:55) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.resources import files
>>> files("notebook")
PosixPath('/usr/lib/python3.9/site-packages/notebook')
>>> files("notebook.templates")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/importlib/resources.py", line 147, in files
    return _common.from_package(_get_package(package))
  File "/usr/lib64/python3.9/importlib/_common.py", line 14, in from_package
    return fallback_resources(package.__spec__)
  File "/usr/lib64/python3.9/importlib/_common.py", line 18, in fallback_resources
    package_directory = pathlib.Path(spec.origin).parent
  File "/usr/lib64/python3.9/pathlib.py", line 1082, in __new__
    self = cls._from_parts(args, init=False)
  File "/usr/lib64/python3.9/pathlib.py", line 707, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/usr/lib64/python3.9/pathlib.py", line 691, in _parse_args
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType
>>> from importlib_resources import files
>>> files("notebook.templates")
MultiplexedPath('/usr/lib/python3.9/site-packages/notebook/templates')
>>> 
abuild@skylab:~/rpmbuild/BUILD/notebook-7.0.2> python3.10
Python 3.10.12 (main, Jun 28 2023, 17:56:55) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.resources import files
>>> files("notebook")
PosixPath('/usr/lib/python3.10/site-packages/notebook')
>>> files("notebook.templates")
MultiplexedPath('/usr/lib/python3.10/site-packages/notebook/templates')
>>> 

Reproduce

python3.9 -m pytest

Expected behavior

Successful test suite

Context

  • Operating System and version: openSUSE Tumbleweed rpmbuild
  • Browser and version: N/A
  • Jupyter Notebook version: 7.0.2
Troubleshoot Output
abuild@skylab:~/rpmbuild/BUILD/notebook-7.0.2> jupyter-troubleshoot-3.9
$PATH:
        /usr/local/bin
        /usr/bin
        /bin

sys.path:
/usr/bin
/usr/lib64/python39.zip
/usr/lib64/python3.9
/usr/lib64/python3.9/lib-dynload
/usr/lib64/python3.9/site-packages
/usr/lib64/python3.9/_import_failed
/usr/lib/python3.9/site-packages

sys.executable:
/usr/bin/python3.9

sys.version:
3.9.17 (main, Jun 28 2023, 19:17:55) [GCC]

platform.platform():
Linux-6.4.9-1-default-x86_64-with-glibc2.38

which -a jupyter:
/usr/bin/jupyter
/bin/jupyter

pip list:
Package Version
------------------------- -----------
anyio 3.6.2
apipkg 3.0.1
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
arrow 1.2.3
asttokens 2.2.1
async-lru 2.0.4
attrs 23.1.0
Babel 2.12.1
backcall 0.2.0
beautifulsoup4 4.12.2
bleach 6.0.0
certifi 2023.7.22
cffi 1.15.1
charset-normalizer 3.2.0
comm 0.1.4
coverage 7.3.0
cryptography 41.0.3
debugpy 1.6.6
decorator 5.1.1
defusedxml 0.7.1
editables 0.5
exceptiongroup 1.1.2
executing 1.2.0
fastjsonschema 2.17.1
fqdn 1.5.1
hatch-jupyter-builder 0.8.3
hatchling 1.18.0
html5lib 1.1
idna 3.4
importlib-metadata 6.8.0
importlib-resources 6.0.1
iniconfig 2.0.0
ipykernel 6.25.1
ipython 8.14.0
isoduration 20.11.0
jedi 0.19.0
Jinja2 3.1.2
json5 0.9.13
jsonpointer 2.3
jsonschema 4.18.6
jsonschema-spec 0.2.3
jsonschema-specifications 2023.7.1
jupyter_client 8.3.0
jupyter_core 5.3.1
jupyter-events 0.6.3
jupyter-lsp 2.2.0
jupyter_server 2.6.0
jupyter_server_terminals 0.4.4
jupyterlab 4.0.5
jupyterlab-pygments 0.2.2
jupyterlab_server 2.24.0
lazy-object-proxy 1.9.0
MarkupSafe 2.1.3
matplotlib-inline 0.1.6
mistune 2.0.5
nbclient 0.8.0
nbconvert 7.7.3
nbformat 5.9.2
nbval 0.10.0
nest-asyncio 1.5.6
notebook 7.0.2
notebook_shim 0.2.3
openapi-schema-validator 0.6.0
openapi-spec-validator 0.6.0
overrides 7.3.1
packaging 23.1
pandocfilters 1.5.0
parso 0.8.3
pathable 0.4.3
pathspec 0.11.1
pexpect 4.8.0
pickleshare 0.7.5
pip 23.1.2
platformdirs 3.5.1
pluggy 1.0.0
prometheus-client 0.17.0
prompt-toolkit 3.0.38
psutil 5.9.5
ptyprocess 0.7.0
pure-eval 0.2.2
py 1.11.0
pycparser 2.21
Pygments 2.16.1
pyOpenSSL 23.2.0
pyrsistent 0.19.3
pytest 7.4.0
pytest-console-scripts 1.4.1
pytest-jupyter 0.7.0
pytest-timeout 2.1.0
pytest-tornasync 0.6.0.post2
python-dateutil 2.8.2
python-json-logger 2.0.7
pytz 2023.3
PyYAML 6.0.1
pyzmq 25.1.1
referencing 0.30.2
requests 2.31.0
requests-mock 1.10.0
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rpds-py 0.7.1
Send2Trash 1.8.0
setuptools 67.8.0
six 1.16.0
sniffio 1.3.0
soupsieve 2.4.1
stack-data 0.6.2
strict-rfc3339 0.7
terminado 0.17.1
tinycss2 1.2.1
tomli 2.0.1
tornado 6.3.2
traitlets 5.9.0
trove-classifiers 2023.5.2
typing_extensions 4.7.1
uri-template 1.2.0
urllib3 2.0.4
wcwidth 0.2.6
webcolors 1.13
webencodings 0.5.1
websocket-client 1.5.1
Werkzeug 2.3.6
zipp 3.15.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions