Skip to main content

Find __version__ for __init__.py

Project description

Project-Version-Finder

Simple package to provide version informaion. The stratagy is

  • first look in pyproject.toml, if it is found. This is most accurate during develompent, but when running from an install it probably will not be found, so
  • second, use importlib.metadata (or importlib_metadata, if appropriate), which works if it is installed.

All you should need is

from project_version_finder import get_version
__version__ = get_version('project-name')

(and make project-version-finder a dependancy of your project).

If you do not want the search for pyproject.toml to start in the current working directory (which is often where you will automatically find your project in development in the python path), you can add a second argument specifying a file in the directory you wish to begin the search. Some suggestions (which may require all version tests during development to NOT have an installed version also available) are:

from project_version_finder import get_version
# start search in directory of the file that defines __version__
__version__ = get_version('project-name', __file__)

or

from project_version_finder import get_version
# start search in directory of the file that defines some function fn
__version__ = get_version('project-name', fn.__globals__['__file__'])

The key is to have a way to get the name of a file in the directory from which you wish the search to start. Bonus points if (like the default current working directory) this name will not follow the install files: then the result will work during development even if you have an older version installed.

It isn't fool proof, but since you provide the project name, if it finds the wrong pyproject.toml it should only fail if that happens to be for a different project with the same name.

Repository
PyPI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

project_version_finder-0.2.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

project_version_finder-0.2.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file project_version_finder-0.2.1.tar.gz.

File metadata

  • Download URL: project_version_finder-0.2.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for project_version_finder-0.2.1.tar.gz
Algorithm Hash digest
SHA256 8ff06f8514497a5c0929093698af6d7fc97d12ca2557260cb6d28d9ec391e152
MD5 45367da5788d2ebc774a264d47236bb1
BLAKE2b-256 ac913447a13a6c229412c2bf17b0fdd82ba11a01b727b5a357770e4dc985ae74

See more details on using hashes here.

File details

Details for the file project_version_finder-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for project_version_finder-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b32aba6b21aa448b0b0a1b295815d19eca50f32a07b10ab0caeeadd0d861786c
MD5 a08035b4ba9106a6516cb1afe7716129
BLAKE2b-256 518e8b5e7855a81126f5e4e5851399e5b6e4c12149502ef2e4569b0977cd4e13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page