Python "importlib.metadata.PackageNotFoundError" in CI
Code asked importlib.metadata for a package’s version or metadata and none was found in the active environment. The distribution’s *.dist-info is not installed where this interpreter looks - usually a missing install or a wrong environment.
What this error means
A program crashes at startup with importlib.metadata.PackageNotFoundError: No package metadata was found for X, often from a version("X") call used to set __version__. The import of the module itself may succeed; only the metadata lookup fails.
importlib.metadata.PackageNotFoundError: No package metadata was found
for my-app
File ".../__init__.py", line 3, in <module>
__version__ = version("my-app")Common causes
The distribution is not installed
Running from a source checkout without pip install means the code is importable but has no installed dist-info, so metadata lookups fail.
Name mismatch or wrong environment
The string passed to version() is the import name, not the distribution name (e.g. version("PIL") vs Pillow), or the script runs under a different venv than where the package was installed.
How to fix it
Install the project so its metadata exists
An editable install registers the dist-info so importlib.metadata can find it even when running from source.
pip install -e .
python -c "from importlib.metadata import version; print(version('my-app'))"Use the distribution name and guard the lookup
Pass the real distribution name, and fall back gracefully when running uninstalled.
from importlib.metadata import version, PackageNotFoundError
try:
__version__ = version("my-app") # distribution name, not import name
except PackageNotFoundError:
__version__ = "0.0.0+unknown"How to prevent it
- Install the project (editable is fine) before running code that reads its own metadata.
- Pass the distribution name, not the import name, to
version(). - Guard metadata lookups so an uninstalled checkout degrades gracefully.