Skip to content

Instantly share code, notes, and snippets.

@benkehoe
Last active June 22, 2022 13:31
Show Gist options
  • Save benkehoe/066a73903e84576a8d6d911cfedc2df6 to your computer and use it in GitHub Desktop.
Save benkehoe/066a73903e84576a8d6d911cfedc2df6 to your computer and use it in GitHub Desktop.
Single sourcing a python package version using importlib.metadata.version()
# MIT No Attribution
#
# Copyright 2022 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# You can single-source your package version setting the package version only in
# the project data in `pyproject.toml` or `setup.py`, using `importlib.metadata`.
#
# If your package requires Python 3.8 or above, you can use the standard library
# package `importlib.metadata` and not add a dependency.
#
# If you're supporting versions below 3.8, you need to add a dependency for the
# shim package `importlib-metadata` and import it if `importlib.metadata`
# is not present.
#
# You use the `version()` function to retrieve the version string for a package.
# The value of `__name__` normally provides the package name, but if you're
# running the package as a script (either `python -m my_package` or through
# a script installed with the package), `__name__` will be `"__main__"`,
# in which case you need to use `__package__` to get the package name.
# As far as I can tell there isn't a variable that covers both situations.
#
# Set the package version in your pyproject.toml or setup.py. If you're
# supporting Python versions before 3.8, add a conditional dependency for
# importlib-metadata (examples below).
#
# Choose one of the following code snippets, depending on what Python versions
# your package supports.
# Put the snippet in the __init__.py of your top-level package
###### supporting Python versions below 3.8 ######
try:
# importlib.metadata is present in Python 3.8 and later
import importlib.metadata as importlib_metadata
except ImportError:
# use the shim package importlib-metadata pre-3.8
import importlib_metadata as importlib_metadata
try:
# __package__ allows for the case where __name__ is "__main__"
__version__ = importlib_metadata.version(__package__ or __name__)
except importlib_metadata.PackageNotFoundError:
__version__ = "0.0.0"
###### supporting only Python 3.8 and above ######
import importlib.metadata
try:
# __package__ allows for the case where __name__ is "__main__"
__version__ = importlib.metadata.version(__package__ or __name__)
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"
# If you're using a pyproject.toml, pick the appropriate format below
#
# importlib-metadata dependency is only necessary if your package supports
# Python versions before 3.8
### poetry ###
[tool.poetry]
name = "my_package"
version = "1.2.3"
# add this in addition to whatever else you've got in here
[tool.poetry.dependencies]
importlib-metadata = { version = "~=1.0", python = "<3.8" }
### setuptools ###
[metadata]
name = "my_package"
version = "1.2.3"
# add this in addition to whatever else you've got in here
[options]
install_requires = "importlib-metadata ~= 1.0 ; python_version < '3.8'"
### PEP621 ###
[project]
name = "my_package"
version = "1.2.3"
# add this in addition to whatever else you've got in here
dependencies = [
"importlib-metadata ~= 1.0 ; python_version < '3.8'"
]
# If you're using setup.py, you either add the following string to the install_requires list
# "importlib-metadata ~= 1.0 ; python_version < '3.8'"
# or include that string under options.install_requires in setup.cfg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment