导入错误Python2.7。没有命名的模块:

2024-05-19 10:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行主脚本时收到一个重要的错误消息,该脚本与另一个试图导入我的一个模块的脚本相关,我不确定如何修复它。与软件相关的文件布局如下(文件夹名称等是虚构的):

poetry_generator/main.py

poetry_generator/architecture/experts/poetryexperts/sentence.py

poetry_generator/structures/word.py

Main.py是我正在运行的,问题似乎是由于sentence.py试图导入word模块而失败引起的。

目前在sentence.py中我有:

from poetry_generator.structures.word import Word

Word是Word.py中类的名称:类Word(object)。但我收到以下错误:ImportError: No module named poetry_generator.structures.word

有人知道怎么回事吗?我一直在读一个鲁顿类似的问题已经提出,但迄今为止没有任何工作。提前谢谢你的帮助。

完整控制台文本重排错误:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from architecture.control_component import ControlComponent
  File "/home/lee/Downloads/PoEmo-master/poetry_generator/architecture/control_component.py", line 4, in <module>
    from experts.poem_making_experts import exclamation_expert
  File "/home/lee/Downloads/PoEmo-master/poetry_generator/architecture/experts/poem_making_experts/exclamation_expert.py", line 5, in <module>
    from poetry_generator.structures.word import Word
ImportError: No module named poetry_generator.structures.word

Tags: frompyimport脚本poetry错误generatorsentence
2条回答

你需要

import sys
sys.path.insert(0, 'System/structures/word')
#or
sys.path.append('System/structures/word')

import Word

否则,您将需要__init__.py,您可以用these instructions制作。

顶层项目目录不应包含在模块名称中。这应该有效:

from structures.word import Word

相关问题 更多 >

    热门问题