Skip to content

Commit fb4b0af

Browse files
committed
fix(pmpm): import pmpm for post-install-hook
1 parent c868ff4 commit fb4b0af

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

python/pminit/post-install-hook.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import os
2-
from . import pmpm
2+
import sys
3+
import importlib
4+
import importlib.util
35

46
WORK_DIR = os.path.join(
5-
os.path.realpath(os.path.dirname(__file__)),
6-
"pythonmonkey"
7+
os.path.realpath(os.path.dirname(__file__)),
8+
"pythonmonkey"
79
)
810

11+
def import_file(module_name: str, file_path: str):
12+
"""
13+
Import a Python file from its relative path directly.
14+
15+
See https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
16+
"""
17+
spec = importlib.util.spec_from_file_location(module_name, file_path)
18+
module = importlib.util.module_from_spec(spec) # type: ignore
19+
sys.modules[module_name] = module
20+
spec.loader.exec_module(module) # type: ignore
21+
return module
22+
923
def main():
10-
pmpm.main(WORK_DIR) # cd pythonmonkey && npm i
24+
pmpm = import_file("pmpm", os.path.join(os.path.dirname(__file__), "./pmpm.py")) # from . import pmpm
25+
pmpm.main(WORK_DIR) # cd pythonmonkey && npm i
1126

1227
if __name__ == "__main__":
13-
main()
28+
main()

0 commit comments

Comments
 (0)