Skip to content

Commit 1497bcd

Browse files
committed
Provide package with __version__ attribute
1 parent 612a76c commit 1497bcd

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pyroids = [
6363
]
6464

6565
[tool.setuptools_scm]
66-
write_to = "_version.py"
66+
write_to = "pyroids/_version.py"
6767

6868
[[tool.uv.index]]
6969
name = "testpypi"

pyroids/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
from __future__ import annotations
6262

63+
import contextlib
6364
from enum import Enum
6465
from pathlib import Path
6566

@@ -79,3 +80,25 @@ class PlayerColor(Enum):
7980

8081
BLUE = "blue"
8182
RED = "red"
83+
84+
85+
# Resolve version
86+
__version__ = None
87+
88+
from importlib.metadata import version # noqa: E402
89+
90+
# get version from installed package
91+
with contextlib.suppress(ImportError):
92+
__version__ = version("pyroids")
93+
94+
if __version__ is None:
95+
try:
96+
# if package not installed, get version as set when package built
97+
from ._version import version
98+
except Exception: # noqa: BLE001, S110
99+
# If package not installed and not built, leave __version__ as None
100+
pass
101+
else:
102+
__version__ = version
103+
104+
del version

0 commit comments

Comments
 (0)