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.2.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.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: project_version_finder-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 d43a7f2229fc499dcd859530bfc7ca9a32edde2c3a8acdcc008374417923aaf5
MD5 67147d720308eec479affa7900bd3447
BLAKE2b-256 4633005b1ed49aa25b27034b5c014fb25d0d7699f80d00b5c1693d3f60673808

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for project_version_finder-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5ca1772f00bc942bb97340d00b46361321af8ba996f29ddc6b5369c7eb24c6a
MD5 e8f49c6a41738ca2c10d18033886232d
BLAKE2b-256 f016619ab723b23e2d6120c95f2163fb9f37b3465380e9328fa96b5a41ee2d35

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