Python: ModuleNotFoundError: No module named ''

Python: ModuleNotFoundError: No module named ''

我是 运行 python docker 中的代码。 我有以下文件结构:

-my_dir   
  -test.py   
  -bird.py   
  -string_int_label_map_pb2.py   
  -inference.py

inference.py:

import test
import bird
from string_int_label_map_pb2 import StringIntLabelMap

test.py 和 bird.py 都包含此代码:

print('hello world!')
def this_is_test():
    return 'hi'

In inference.py 'import test' 不会抛出错误(但 'hello world!' 永远不会打印)。 'import bird' 抛出:ModuleNotFoundError:没有名为 'bird' 的模块。最后 'import string_int_label_map_pb2 ' 抛出:ModuleNotFoundError:没有名为 'string_int_label_map_pb2 '

的模块

注意:bird.py 和 test.py 并没有同时存在于目录中,它们被这样描述只是为了说明我的观点。只有当我将同一个文件重命名为任一名称以测试名称本身是否是罪魁祸首时,它们才存在。

我将bird.py添加到/worker/(dockerfile中配置的WORKDIR路径),果然“Hello world!”印刷。

所以问题是 inference.py 没有像我想的那样在其父目录中搜索导入,而是在 WORKDIR 中配置的路径。

仍然不明白为什么导入测试没有出错,因为 /worker/ 中从来没有 test.py 文件。

如果其中一个不在文件夹中,但 inference.py 中仍有 import test 和 import bird,那是你的问题。 Python 正在尝试导入这两个文件,但找不到文件。

我也遇到了这个问题,请问是怎么解决的? 例如。 我的目录:

--申请 ---挂钩 ----init.py ----helpers.py - -模型 ----init.py ----model_person.py

在脚本中 model_person.py

导入系统 sys.path.append('../') 来自 hooks.helpers 导入 *

就这些