Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: napari does not start with Python v3.11.1: "ValueError: A distribution name is required." #5482

Merged
merged 3 commits into from Feb 17, 2023

Conversation

glichtner
Copy link
Contributor

Description

Summary

napari does not start with Python 3.11.1 as importlib.metadata.Distribution raises ValueError: A distribution name is required.

Solution

The solution is to add an additional check for empty app_module in running_as_bundled_app() and return False if it is empty.

Details

  • The from_name() function of Python's importlib.metadata.Distribution has been changed to check for the emptiness of the name parameter in Python 3.11.1 (vs 3.11.0; see updated version here vs previous version ).
  • An empty name will now throw a ValueError, preventing napari from starting with Python v3.11.1, as napari.utils.misc.running_as_bundled_app() calls importlib.metadata.metadata() with an empty app_module parameter when running as stand-alone or using pytest.
  • This app_module parameter is directly relayed to Distribution.from_name, resulting in the following error when running napari with Python 3.11.1 (Ubuntu 22.04.1 LTS; napari pypi release 0.4.17 and github commit 54c1dbf )
Traceback (most recent call last):
  File "/home/glichtner/code/test/test/bin/napari", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/__main__.py", line 561, in main
    _run()
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/__main__.py", line 218, in _run
    from napari import Viewer, run
  File "<frozen importlib._bootstrap>", line 1231, in _handle_fromlist
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/_lazy.py", line 48, in __getattr__
    submod = import_module(
             ^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/viewer.py", line 8, in <module>
    from .components.viewer_model import ViewerModel
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/components/__init__.py", line 19, in <module>
    from .layerlist import LayerList
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/components/layerlist.py", line 9, in <module>
    from ..layers import Layer
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/layers/__init__.py", line 11, in <module>
    from .base import Layer
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/layers/base/__init__.py", line 1, in <module>
    from .base import Layer, no_op
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/layers/base/base.py", line 28, in <module>
    from ...utils.key_bindings import KeymapProvider
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/utils/key_bindings.py", line 46, in <module>
    from ..settings import get_settings
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/settings/__init__.py", line 6, in <module>
    from ._napari_settings import CURRENT_SCHEMA_VERSION, NapariSettings
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/settings/_napari_settings.py", line 14, in <module>
    from ._plugins import PluginsSettings
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/settings/_plugins.py", line 27, in <module>
    class PluginsSettings(EventedSettings):
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/settings/_plugins.py", line 75, in PluginsSettings
    class NapariConfig:
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/settings/_plugins.py", line 83, in NapariConfig
    if running_as_bundled_app() or running_as_constructor_app():
       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/glichtner/code/test/test/lib/python3.11/site-packages/napari/utils/misc.py", line 67, in running_as_bundled_app
    metadata = importlib.metadata.metadata(app_module)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/metadata/__init__.py", line 998, in metadata
    return Distribution.from_name(distribution_name).metadata
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/metadata/__init__.py", line 561, in from_name
    raise ValueError("A distribution name is required.")
ValueError: A distribution name is required.

Type of change

  • Bug-fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

References

How has this been tested?

  • example: the test suite for my feature covers cases x, y, and z
  • example: all tests pass with my change
  • example: I check if my changes works with both PySide and PyQt backends
    as there are small differences between the two Qt bindings.

Final checklist:

  • My PR is the minimum possible work for the desired functionality
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • If I included new strings, I have used trans. to make them localizable.
    For more information see our translations guide.

@psobolewskiPhD
Copy link
Member

Thanks for the contribution!
🙏
I've activated CI and requested reviews from some smart folks!

@codecov
Copy link

codecov bot commented Jan 15, 2023

Codecov Report

Merging #5482 (1b5ae32) into main (6d27f81) will decrease coverage by 0.06%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #5482      +/-   ##
==========================================
- Coverage   89.40%   89.35%   -0.06%     
==========================================
  Files         609      609              
  Lines       51146    51148       +2     
==========================================
- Hits        45729    45705      -24     
- Misses       5417     5443      +26     
Impacted Files Coverage Δ
napari/utils/misc.py 81.89% <100.00%> (+0.15%) ⬆️
napari/_tests/test_draw.py 36.84% <0.00%> (-63.16%) ⬇️
napari/components/experimental/chunk/_pool.py 85.71% <0.00%> (-7.94%) ⬇️
napari/_qt/widgets/qt_color_swatch.py 71.54% <0.00%> (-3.26%) ⬇️
napari/utils/interactions.py 72.64% <0.00%> (-2.84%) ⬇️
napari/utils/info.py 81.91% <0.00%> (-2.13%) ⬇️
napari/components/_tests/test_layer_slicer.py 97.98% <0.00%> (-1.21%) ⬇️
napari/_qt/qt_event_loop.py 80.27% <0.00%> (-0.69%) ⬇️
napari/_qt/dialogs/qt_package_installer.py 81.81% <0.00%> (+0.39%) ⬆️
napari/utils/theme.py 94.04% <0.00%> (+0.59%) ⬆️
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@Czaki
Copy link
Collaborator

Czaki commented Jan 15, 2023

PR related to this PR: #5439,

EDIT. IT will be nice to add some CI run for python 3.11 to our matrix. Without this we do not know if it is the only bug in 3.11 env.

@jaimergp
Copy link
Contributor

This LGTM, but I have a question: under what circumstances is __package__ set to None?

@Czaki Czaki added the bugfix PR with bugfix label Feb 10, 2023
@Czaki Czaki added this to the 0.5.0 milestone Feb 10, 2023
@Czaki Czaki added the ready to merge Last chance for comments! Will be merged in ~24h label Feb 10, 2023
@Czaki
Copy link
Collaborator

Czaki commented Feb 10, 2023

start 24h clock

This LGTM, but I have a question: under what circumstances is __package__ set to None?

I do not know, but it is present for a long time.

This what was changed is importlib.metadata.metadata that stops accepting None as the argument.

@jni jni merged commit c6f37e6 into napari:main Feb 17, 2023
@Czaki Czaki removed the ready to merge Last chance for comments! Will be merged in ~24h label Feb 18, 2023
@Czaki Czaki modified the milestones: 0.5.0, 0.4.18 May 23, 2023
@Czaki Czaki mentioned this pull request Jun 7, 2023
Czaki added a commit that referenced this pull request Jun 18, 2023
…tribution name is required." (#5482)

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Czaki added a commit that referenced this pull request Jun 19, 2023
…tribution name is required." (#5482)

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Czaki added a commit that referenced this pull request Jun 21, 2023
…tribution name is required." (#5482)

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Czaki added a commit that referenced this pull request Jun 21, 2023
…tribution name is required." (#5482)

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Czaki added a commit that referenced this pull request Jun 21, 2023
…tribution name is required." (#5482)

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR with bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants