fix(version): fall back to pyproject in source checkouts
Keep importlib.metadata as the primary source for installed packages, but avoid PackageNotFoundError when nanobot is imported directly from a source tree. Made-with: Cursor
This commit is contained in:
+22
-2
@@ -2,9 +2,29 @@
|
||||
nanobot - A lightweight AI agent framework
|
||||
"""
|
||||
|
||||
from importlib.metadata import version as _pkg_version
|
||||
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
||||
from pathlib import Path
|
||||
import tomllib
|
||||
|
||||
__version__ = _pkg_version("nanobot-ai")
|
||||
|
||||
def _read_pyproject_version() -> str | None:
|
||||
"""Read the source-tree version when package metadata is unavailable."""
|
||||
pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
|
||||
if not pyproject.exists():
|
||||
return None
|
||||
data = tomllib.loads(pyproject.read_text(encoding="utf-8"))
|
||||
return data.get("project", {}).get("version")
|
||||
|
||||
|
||||
def _resolve_version() -> str:
|
||||
try:
|
||||
return _pkg_version("nanobot-ai")
|
||||
except PackageNotFoundError:
|
||||
# Source checkouts often import nanobot without installed dist-info.
|
||||
return _read_pyproject_version() or "0.1.5"
|
||||
|
||||
|
||||
__version__ = _resolve_version()
|
||||
__logo__ = "🐈"
|
||||
|
||||
from nanobot.nanobot import Nanobot, RunResult
|
||||
|
||||
Reference in New Issue
Block a user