Skip to content

Commit 8efa31f

Browse files
FredHaaWauplin
andauthored
Add expanduser and expandvars to path envvars (#2945)
* Add expanduser and expandvars to path envvars * Update src/huggingface_hub/constants.py --------- Co-authored-by: Lucain <[email protected]>
1 parent 3e72731 commit 8efa31f

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/huggingface_hub/constants.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ def _as_int(value: Optional[str]) -> Optional[int]:
114114

115115
# default cache
116116
default_home = os.path.join(os.path.expanduser("~"), ".cache")
117-
HF_HOME = os.path.expanduser(
118-
os.getenv(
119-
"HF_HOME",
120-
os.path.join(os.getenv("XDG_CACHE_HOME", default_home), "huggingface"),
117+
HF_HOME = os.path.expandvars(
118+
os.path.expanduser(
119+
os.getenv(
120+
"HF_HOME",
121+
os.path.join(os.getenv("XDG_CACHE_HOME", default_home), "huggingface"),
122+
)
121123
)
122124
)
123125
hf_cache_home = HF_HOME # for backward compatibility. TODO: remove this in 1.0.0
@@ -130,8 +132,22 @@ def _as_int(value: Optional[str]) -> Optional[int]:
130132
HUGGINGFACE_ASSETS_CACHE = os.getenv("HUGGINGFACE_ASSETS_CACHE", default_assets_cache_path)
131133

132134
# New env variables
133-
HF_HUB_CACHE = os.getenv("HF_HUB_CACHE", HUGGINGFACE_HUB_CACHE)
134-
HF_ASSETS_CACHE = os.getenv("HF_ASSETS_CACHE", HUGGINGFACE_ASSETS_CACHE)
135+
HF_HUB_CACHE = os.path.expandvars(
136+
os.path.expanduser(
137+
os.getenv(
138+
"HF_HUB_CACHE",
139+
HUGGINGFACE_HUB_CACHE,
140+
)
141+
)
142+
)
143+
HF_ASSETS_CACHE = os.path.expandvars(
144+
os.path.expanduser(
145+
os.getenv(
146+
"HF_ASSETS_CACHE",
147+
HUGGINGFACE_ASSETS_CACHE,
148+
)
149+
)
150+
)
135151

136152
HF_HUB_OFFLINE = _is_true(os.environ.get("HF_HUB_OFFLINE") or os.environ.get("TRANSFORMERS_OFFLINE"))
137153

@@ -146,7 +162,14 @@ def _as_int(value: Optional[str]) -> Optional[int]:
146162
or _is_true(os.environ.get("DO_NOT_TRACK")) # https://consoledonottrack.com/
147163
)
148164

149-
HF_TOKEN_PATH = os.environ.get("HF_TOKEN_PATH", os.path.join(HF_HOME, "token"))
165+
HF_TOKEN_PATH = os.path.expandvars(
166+
os.path.expanduser(
167+
os.getenv(
168+
"HF_TOKEN_PATH",
169+
os.path.join(HF_HOME, "token"),
170+
)
171+
)
172+
)
150173
HF_STORED_TOKENS_PATH = os.path.join(os.path.dirname(HF_TOKEN_PATH), "stored_tokens")
151174

152175
if _staging_mode:

0 commit comments

Comments
 (0)