Skip to content

ModuleDirImport only finds distribution metadata if module has already been loaded #177

@chrisnovakovic

Description

@chrisnovakovic

In the pex bootstrap code, the ModuleDirImport class is able to act as a meta path finder owing to its implementation of find_distributions, but only does so if the distribution metadata lookup (e.g. via importlib.metadata.version) occurs after the module in question has been imported by the interpreter. The root cause is the check on whether the module name exists in sys.modules, which for pex-packed modules will have been added in load_module:

sys.modules[fullname] = module

if context.name in sys.modules:

Ultimately, it means that while this succeeds:

import importlib.metadata
import somemodule

importlib.metadata.version("somemodule")

This fails:

import importlib.metadata

importlib.metadata.version("somemodule")
# importlib.metadata.PackageNotFoundError: No package metadata was found for somemodule

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions