我正在尝试以编程方式导入子模块。我的文件树如下所示:
oopsd/__init__.py
oopsd/oopsd.py
oopsd/driver/__init__.py
oopsd/driver/optiups.py
optiups.py 只打印“Hello World”。
oopsd.py 看起来像这样:
import importlib
importlib.import_module('oopsd.driver.optiups')
现在有了这个,我得到了这个例外:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1521, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "oopsd/oopsd.py", line 29, in <module>
sys.exit(main())
File "oopsd/oopsd.py", line 23, in main
loaddriver()
File "oopsd/oopsd.py", line 26, in loaddriver
importlib.import_module('oopsd.driver.optiups')
File "/usr/lib/python3.3/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
File "<frozen importlib._bootstrap>", line 1514, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
File "<frozen importlib._bootstrap>", line 1524, in _find_and_load_unlocked
ImportError: No module named 'oopsd.driver'; oopsd is not a package
Python 3 中是否__path__
还存在?
我也尝试过导入.driver.optiups
,但这会产生:
TypeError: relative imports require the 'package' argument
__package__
似乎不稳定,所以我迷路了。
我该如何以正确的方式做到这一点?