Skip to content

Consider using importlib.metadata for package versions #6

@neilflood

Description

@neilflood

Currently makeAutomaticFields() looks for package version numbers via the __version__ attribute. However, while this is widely used, it is not always supported, and there is no formal directive to do so. There was PEP 396 on this topic, but it was withdrawn, and the whole question was left to package managers to deal with.

In Python 3.8, the importlib.metadata module was added, which reads package metadata from places like egg-info. Included in this is a version() function, which gives the package manager's notion of the package version, quite distinct from that found in __version__.

I am unsure whether to use this. However, it seems worth considering, either as the primary or fallback method of getting a version number. It does a few odd things, so needs to be handled with care.

  • It raises an exception if it can't find a version number
  • The version() function expects a distribution name, rather than package name. These are often the same, but not always, so one would need to use packages_distributions() to translate between these

I am not going to act on this immediately, but wanted to record the idea, so I remember to investigate it. Any thoughts and comments are welcome.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions