Skip to content

Commit a9b0019

Browse files
authored
Handle setuptools changing sys.path for importing packaging.version (#344)
1 parent 36db52e commit a9b0019

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

logfire/_internal/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,21 @@ def get_version(version: str) -> Version:
188188
from packaging.version import Version
189189

190190
except ImportError: # pragma: no cover
191+
# Trigger the sys.path change mentioned below, but discard this.
191192
from setuptools._vendor.packaging.version import Version
193+
194+
try:
195+
# See https://pydanticlogfire.slack.com/archives/C06EDRBSAH3/p1722017944332959
196+
# Importing setuptools modifies sys.path so that `packaging.version` points to the vendored module.
197+
# This means that two calls to this function could return instances of
198+
# `setuptools._vendor.packaging.version.Version` and `packaging.version.Version`
199+
# (the same file but in different module objects) which cannot be compared.
200+
# So first try `packaging.version` again.
201+
from packaging.version import Version
202+
203+
except ImportError:
204+
# sys.path is only changed in newer versions, so fallback to just importing the vendored Version directly.
205+
from setuptools._vendor.packaging.version import Version
192206
return Version(version) # type: ignore
193207

194208

0 commit comments

Comments
 (0)