【发布时间】:2019-10-17 02:48:14
【问题描述】:
由于某种原因,我无法导入某个文件。这是我的目录结构:
root_folder/
program_to_run.py
configuration/
..config files
tools/
__init__.py
tools_to_use1/
__init__.py
dependencies/
__init__.py
helper_1.py
... other .py files
unit_tests/
_init__.py
helper_1_test.py
... other unit tests for dependencies
tools_to_use2/
__init__.py
hardware/
__init__.py
helper_2.py
... other .py files
... other .py files
我尝试将helper_2.py 导入helper_1.py 的位置
在 helper_1.py 我有以下导入语句:
from tools_to_use2.hardware import helper_2.py
但是,当我在 program_to_run.py(在根文件夹中)中使用 helper_1.py 时,出现以下错误:
ModuleNotFoundError: No module named 'tools_to_use2'
我假设这与我打包脚本的方式有关?我遵循这里的结构:https://docs.python.org/3/tutorial/modules.html (6.4 Packages) 但是,我不确定我做错了什么。感谢所有提前回复的人。
我也尝试将模块添加到我的 PYTHON PATH 中,但我仍然没有运气。我也尝试过使用 sys.insert 技巧,但即使这样也不适合我。
【问题讨论】:
-
@West 我已经尝试了其中一些解决方案,但仍然遇到同样的错误。
标签: python-3.x path package importerror