我目前正在开发这项服务,该服务应动态导入python代码文件(模块)并在每个模块中调用函数AttributeError
。问题是,如果我正确理解错误,那就是我在模块Launcher
中导入模块SomeModuleToImport
(模块导致模块导入模块SomeModuleToImport
)
我该如何解决?
此外,每次需要时我都需要重新导入模块,因为SomeModuleToImport
中的代码可以在运行时更改。
Python版本3.6.1。
文件结构:
|
- Launcher.py
- SomeModuleToImport.py
Launcher.py
import importlib
script = importlib.import_module("SomeModuleToImport")
script.handle()
def writeMsg(txt):
print(txt)
SomeModuleToImport.py
import Launcher
def handle(user, req):
Launcher.writeMsg("Hello!")
错误:
Traceback (most recent call last):
File "/Users/lexmint/Dropbox/Archive/Development/Breeze/Launcher.py", line 3, in <module>
script = importlib.import_module("unban")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/lexmint/Dropbox/Archive/Development/Breeze/unban.py", line 1, in <module>
import Launcher
File "/Users/lexmint/Dropbox/Archive/Development/Breeze/Launcher.py", line 4, in <module>
script.handle()
AttributeError: module 'SomeModuleToImport' has no attribute 'handle'