diff --git a/astrapy/__init__.py b/astrapy/__init__.py index 763b99c5..055922b0 100644 --- a/astrapy/__init__.py +++ b/astrapy/__init__.py @@ -14,28 +14,35 @@ import toml import os +import importlib.metadata from typing import Any def get_version() -> Any: - # Get the path to the pyproject.toml file - dir_path = os.path.dirname(os.path.realpath(__file__)) - pyproject_path = os.path.join(dir_path, "..", "pyproject.toml") - - # Read the pyproject.toml file and get the version from the poetry section try: - with open(pyproject_path, encoding="utf-8") as pyproject: - # Load the pyproject.toml file as a dictionary - file_contents = pyproject.read() - pyproject_data = toml.loads(file_contents) - - # Return the version from the poetry section - return pyproject_data["tool"]["poetry"]["version"] - - # If the pyproject.toml file does not exist or the version is not found, return unknown - except (FileNotFoundError, KeyError): - return "unknown" + # Poetry will create a __version__ attribute in the package's __init__.py file + return importlib.metadata.version(__package__) + + # If the package is not installed, we can still get the version from the pyproject.toml file + except importlib.metadata.PackageNotFoundError: + # Get the path to the pyproject.toml file + dir_path = os.path.dirname(os.path.realpath(__file__)) + pyproject_path = os.path.join(dir_path, "..", "pyproject.toml") + + # Read the pyproject.toml file and get the version from the poetry section + try: + with open(pyproject_path, encoding="utf-8") as pyproject: + # Load the pyproject.toml file as a dictionary + file_contents = pyproject.read() + pyproject_data = toml.loads(file_contents) + + # Return the version from the poetry section + return pyproject_data["tool"]["poetry"]["version"] + + # If the pyproject.toml file does not exist or the version is not found, return unknown + except (FileNotFoundError, KeyError): + return "unknown" __version__ = get_version() diff --git a/pyproject.toml b/pyproject.toml index 43c33962..c4b48980 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "astrapy" -version = "0.7.2" +version = "0.7.2.post1" description = "AstraPy is a Pythonic SDK for DataStax Astra" authors = [ "Kirsten Hunter "