Coverage for trimesh/version.py: 50%
18 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-24 04:40 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-24 04:40 +0000
1"""
2# version.py
4Get the current version from package metadata or pyproject.toml
5if everything else fails.
6"""
8import json
9import os
12def _get_version() -> str | None:
13 """
14 Try all our methods to get the version.
15 """
17 try:
18 # Get the version string using package metadata
19 from importlib.metadata import version
21 return version("trimesh")
22 except Exception:
23 pass
25 try:
26 # Get the version string from the pyproject.toml file using
27 # relative paths. This will be the only option if the library
28 # has not been installed (i.e. in a CI or container environment)
29 pyproject = os.path.abspath(
30 os.path.join(
31 os.path.dirname(os.path.abspath(os.path.expanduser(__file__))),
32 "..",
33 "pyproject.toml",
34 )
35 )
36 with open(pyproject) as f:
37 # json.loads cleans up the string and removes the quotes
38 # this logic requires the first use of "version" be the actual version
39 return next(json.loads(L.split("=")[1]) for L in f if "version" in L)
40 except Exception:
41 pass
43 return None
46# try all our tricks
47__version__ = _get_version()
49if __name__ == "__main__":
50 # print version if run directly i.e. in a CI script
51 print(__version__) # noqa: T201