Skip to content

Commit 3d71fd1

Browse files
authored
Merge pull request #431 from evanzd/fix_get_module
Fix `get_module_by_module_path` to support pickle module loaded from arbitrage source file
2 parents 19eda8f + 2fa7ef3 commit 3d71fd1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

qlib/utils/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import pickle
1010
import re
11+
import sys
1112
import copy
1213
import json
1314
import yaml
@@ -178,8 +179,10 @@ def get_module_by_module_path(module_path: Union[str, ModuleType]):
178179
module = module_path
179180
else:
180181
if module_path.endswith(".py"):
181-
module_spec = importlib.util.spec_from_file_location("", module_path)
182+
module_name = re.sub("^[^a-zA-Z_]+", "", re.sub("[^0-9a-zA-Z_]", "", module_path[:-3].replace("/", "_")))
183+
module_spec = importlib.util.spec_from_file_location(module_name, module_path)
182184
module = importlib.util.module_from_spec(module_spec)
185+
sys.modules[module_name] = module
183186
module_spec.loader.exec_module(module)
184187
else:
185188
module = importlib.import_module(module_path)

0 commit comments

Comments
 (0)