Skip to content

Package breaks when importlib metadata isn't available (Pyinstaller) #1042

Closed

Description

@Phillip-Miller

Problem to Solve

__init__.py calls importlib.metadata.version() at import time :

This breaks when the package metadata isn't bundled (I use PyInstaller and run into this issue) . I can get around this fine by bundling metadata or doing other hacks, but I think just using a try check is the correct approach.

Proposed Solution

Fall back to unknown when the metadata is missing. I saw this pattern being used in the PyVisa repo.

try:
version = version(name)
except PackageNotFoundError:
version = "unknown"

AB#4147314

Activity

  1. bkeryan commented on Nov 4, 2026

    @bkeryan
    Collaborator
  2. bkeryan commented on Nov 11, 2026

    @bkeryan
    Collaborator

    https://github.com/pyinstaller/pyinstaller/blob/master/doc/CHANGES.rst#44-2021-07-13

    Collect distribution metadata automatically. This works by scanning collected Python files for uses of:

    • pkg_resources.get_distribution()
    • pkg_resources.require()
    • importlib.metadata.distribution()
    • importlib.metadata.metadata()
    • importlib.metadata.files()
    • importlib.metadata.version()

    In all cases, the metadata will only be collected if the distribution name is given as a plain string literal. Anything more complex will still require a hook containing :func:PyInstaller.utils.hooks.copy_metadata. (:issue:#5830)

    This wasn't working because we were calling importlib.metadata.version(__name__) rather than importlib.metadata.version("nidaqmx"). Changing it to use a string literal seems to make PyInstaller copy the package metadata.

    I also added the try/except, in case there are other scenarios that run into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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