导入错误Python 2.7。没有命名的模块:

时间:2017-04-24 10:59:09

标签: python python-2.7 ubuntu python-import

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

poetry_generator/main.py

poetry_generator/architecture/experts/poetryexperts/sentence.py

poetry_generator/structures/word.py

Main.py是我正在运行的,问题似乎来自于试图导入word模块并失败的sentence.py。

目前在sentence.py中我有:

from poetry_generator.structures.word import Word

Word是word.py中Class的名称:Class 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

2 个答案:

答案 0 :(得分:4)

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

from structures.word import Word

答案 1 :(得分:2)

你需要

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

import Word

否则,您需要__init__.py才能使用these instructions