Python打包问题

时间:2019-05-31 12:40:22

标签: python-3.x path package importerror

由于某种原因,我无法导入某个文件。这是我的目录结构:

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

但是,当我在helper_1.py(在根文件夹中)中使用program_to_run.py时,出现以下错误:

ModuleNotFoundError: No module named 'tools_to_use2'

我假设这与我打包脚本的方式有关?我在这里遵循以下结构:https://docs.python.org/3/tutorial/modules.html(6.4包)但是,我不确定自己在做什么错。感谢所有提前答复的人。

我也尝试过将模块添加到我的PYTHON PATH中,但是我仍然没有运气。我也尝试过使用sys.insert技巧,但即使那样对我也不起作用。

1 个答案:

答案 0 :(得分:0)

简而言之,这是不可能的。这篇文章对此https://stackoverflow.com/a/30673795/10054278进行了说明。如果您想这样做,则必须找到解决方法。在我最初分享的链接中,有一些可能有效。我建议您重组项目。

相关问题