Skip to content

Commit 407c838

Browse files
authored
use importlib_metadata instead of import torch and tf (#12)
1 parent 7c43735 commit 407c838

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def get_version() -> str:
1515
"filelock",
1616
"requests",
1717
"tqdm",
18+
"importlib_metadata;python_version<'3.8'",
1819
]
1920

2021
extras = {}

src/huggingface_hub/file_download.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@
2323

2424
logger = logging.getLogger(__name__)
2525

26+
_PY_VERSION: str = sys.version.split()[0]
2627

27-
try:
28-
import torch
28+
if tuple(int(i) for i in _PY_VERSION.split(".")) < (3, 8, 0):
29+
import importlib_metadata
30+
else:
31+
import importlib.metadata as importlib_metadata
2932

33+
_torch_version = "N/A"
34+
_torch_available = False
35+
try:
36+
_torch_version = importlib_metadata.version("torch")
3037
_torch_available = True
31-
except ImportError:
32-
_torch_available = False
38+
except importlib_metadata.PackageNotFoundError:
39+
pass
3340

41+
_tf_version = "N/A"
42+
_tf_available = False
3443
try:
35-
import tensorflow as tf
36-
44+
_tf_version = importlib_metadata.version("tensorflow")
3745
_tf_available = True
38-
except ImportError:
39-
_tf_available = False
46+
except importlib_metadata.PackageNotFoundError:
47+
pass
4048

4149

4250
def is_torch_available():
@@ -161,11 +169,11 @@ def http_user_agent(
161169
else:
162170
ua = "unknown/None"
163171
ua += "; hf_hub/{}".format(__version__)
164-
ua += "; python/{}".format(sys.version.split()[0])
172+
ua += "; python/{}".format(_PY_VERSION)
165173
if is_torch_available():
166-
ua += "; torch/{}".format(torch.__version__)
174+
ua += "; torch/{}".format(_torch_version)
167175
if is_tf_available():
168-
ua += "; tensorflow/{}".format(tf.__version__)
176+
ua += "; tensorflow/{}".format(_tf_version)
169177
if isinstance(user_agent, dict):
170178
ua += "; " + "; ".join("{}/{}".format(k, v) for k, v in user_agent.items())
171179
elif isinstance(user_agent, str):

0 commit comments

Comments
 (0)