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.3.0.tar.gz (4.2 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.3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: project_version_finder-0.3.0.tar.gz
  • Upload date:
  • Size: 4.2 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.3.0.tar.gz
Algorithm Hash digest
SHA256 59a13e662d2f5f2fdd33dfdf501ba215543d2e456190364f42611196d5d5d5e3
MD5 c97522c8995d95cecbf6e54e4f54f472
BLAKE2b-256 f055ca323737b30588ee49b9b6be3b295a7f91b438f835bb2492fd45c7e2ea53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for project_version_finder-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe705c8b013ea57e90ff21fd020ff4b8fff1e0db3fe86c3fe205ce54582c42d1
MD5 75dde58d0531b5d82edab685d9022c37
BLAKE2b-256 8a176b69a878e0dc104d8c59c06b6db2b0b474115114ddfbebb345a22ddf15aa

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