回答編集履歴
3
修正
test
CHANGED
@@ -101,3 +101,19 @@
|
|
101
101
|
func() # func called
|
102
102
|
|
103
103
|
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
/data/notebook/test/a.py
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
```python
|
112
|
+
|
113
|
+
class MyClass:
|
114
|
+
|
115
|
+
def func(self):
|
116
|
+
|
117
|
+
print("func called")
|
118
|
+
|
119
|
+
```
|
2
修正
test
CHANGED
@@ -66,21 +66,21 @@
|
|
66
66
|
|
67
67
|
import importlib.util
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
69
|
+
from pathlib import Path
|
72
70
|
|
73
71
|
|
74
72
|
|
75
|
-
module_path = "/data/notebook/test/a.py"
|
73
|
+
module_path = "/data/notebook/test/a.py"
|
76
74
|
|
77
|
-
|
75
|
+
class_name = "MyClass"
|
78
76
|
|
79
|
-
func_name = "func
|
77
|
+
func_name = "func"
|
80
78
|
|
81
79
|
|
82
80
|
|
81
|
+
module_path = Path(module_path)
|
82
|
+
|
83
|
-
spec = importlib.util.spec_from_file_location(module_
|
83
|
+
spec = importlib.util.spec_from_file_location(module_path.stem, module_path)
|
84
84
|
|
85
85
|
module = importlib.util.module_from_spec(spec)
|
86
86
|
|
@@ -88,8 +88,16 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
-
|
91
|
+
MyClass = getattr(module, class_name)
|
92
92
|
|
93
|
+
|
94
|
+
|
95
|
+
inst = MyClass()
|
96
|
+
|
97
|
+
func = getattr(inst, func_name)
|
98
|
+
|
99
|
+
|
100
|
+
|
93
|
-
func()
|
101
|
+
func() # func called
|
94
102
|
|
95
103
|
```
|
1
修正
test
CHANGED
@@ -55,3 +55,41 @@
|
|
55
55
|
print("b.func_b")
|
56
56
|
|
57
57
|
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
## 追記
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
```python
|
66
|
+
|
67
|
+
import importlib.util
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
import importlib
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
module_path = "/data/notebook/test/a.py" # ファイルのある場所
|
76
|
+
|
77
|
+
module_name = "a"
|
78
|
+
|
79
|
+
func_name = "func_a"
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
84
|
+
|
85
|
+
module = importlib.util.module_from_spec(spec)
|
86
|
+
|
87
|
+
spec.loader.exec_module(module)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
func = getattr(module, func_name)
|
92
|
+
|
93
|
+
func()
|
94
|
+
|
95
|
+
```
|