microsoft/qlib · error · ValueError
Cannot find file {}
Error message
Cannot find file {} What it means
Raised by load_instance in qlib/contrib/online/utils.py when the pickle file path passed in does not exist. load_instance deserializes user strategies and models for the online manager, and the user's account folder is expected to contain strategy_<id>.pickle and model_<id>.pickle; a missing file aborts the load before unpickling.
Source
Thrown at qlib/contrib/online/utils.py:32
from ...utils import get_next_trading_date
from ...utils.pickle_utils import restricted_pickle_load
from ...backtest.exchange import Exchange
log = get_module_logger("utils")
def load_instance(file_path):
"""
load a pickle file
Parameter
file_path : string / pathlib.Path()
path of file to be loaded
:return
An instance loaded from file
"""
file_path = pathlib.Path(file_path)
if not file_path.exists():
raise ValueError("Cannot find file {}".format(file_path))
with file_path.open("rb") as fr:
instance = restricted_pickle_load(fr)
return instance
def save_instance(instance, file_path):
"""
save(dump) an instance to a pickle file
Parameter
instance :
data to be dumped
file_path : string / pathlib.Path()
path of file to be dumped
"""
file_path = pathlib.Path(file_path)
with file_path.open("wb") as fr:
pickle.dump(instance, fr, C.dump_protocol_version)
View on GitHub (pinned to 79633dd950)
Solutions
- Inspect the user folder and confirm both strategy_<id>.pickle and model_<id>.pickle exist before load_users().
- If pickles are missing, re-create the user via add_user (with its original config) or restore the files from backup.
- Guard your wrapper: check file_path.exists() before calling load_instance and surface which file is missing.
Example fix
# before
inst = load_instance(user_path / "strategy_u1.pickle") # ValueError: Cannot find file ...
# after
fp = user_path / "strategy_u1.pickle"
if not fp.exists():
raise FileNotFoundError(f"missing pickle: {fp}; re-add the user")
inst = load_instance(fp) Defensive patterns
Strategy: validation
Validate before calling
import pathlib
required = [
user_path / f"strategy_{user_id}.pickle",
user_path / f"model_{user_id}.pickle",
]
missing = [p for p in required if not p.is_file()]
if missing:
raise FileNotFoundError(f"user {user_id} incomplete, missing: {missing}")
inst = load_instance(required[0]) Try / catch
try:
inst = load_instance(fp)
except ValueError as e:
if "Cannot find file" in str(e):
raise FileNotFoundError(fp) from e
raise Prevention
- Validate user folders contain both pickles before load_users().
- Save account, strategy and model atomically (save_user_data) so folders are never partial.
- Back up user data directories; never prune individual pickle files by hand.
When it happens
Trigger: UserManager.create_user/load_users reaching load_instance for a user folder that lacks strategy_*.pickle or model_*.pickle (deleted, never saved via save_user_data, or partially created); or any direct call to load_instance with a bad path.
Common situations: User data folder corrupted or incompletely written (crash between account save and pickle save); manually pruning files inside user folders; moving/renaming user directories so the constructed file name no longer matches.
Related errors
- Cannot find config file {}
- Cannot find user data {}
- No file starting with '{filename_without_suffix}' found
- Can't find the BASE_CONFIG file: {base_config_path}
- User {} has been loaded
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/8ec25f1bdf0a6859.
Report an issue: GitHub.