【问题标题】:importError python while import exists and Pycharm recognize itimportError python while import 存在并且Pycharm识别它
【发布时间】:2019-04-02 08:36:33
【问题描述】:

我正在处理一个项目,但我遇到了一个由于某种原因无法修复的错误。

错误是:

`from parser import WorldParser`
`ImportError: cannot import name WorldParser`

我正在使用 Pycharm,我尝试使用 Pycycle 来查找我是否有导入周期,但它没有找到,还尝试手动查找周期,但我没有找到 任何。

程序结构如下:

project folder contains:
agent.py
graph.py
parser.py
simulation.py
state.py
utils.py

现在我将详细说明每个文件的导入。

-----agent.py----

from utils import operation_dec,get_path_from_to

-----agent.py----

-----graph.py----

none

-----graph.py----

-----parser.py----

from graph import Vertex, Edge, UndirectedGraph

from state import WorldState

from utils import create_map_args, get_path_from_to

-----parser.py----

-----simulation.py----

from parser import WorldParser

from beautifultable import BeautifulTable

from agent import GreedyAgent, HumanAgent, VandalAgent

from state import WorldState

from utils import InfoObject

-----simulation.py----

-----state.py----

from beautifultable import BeautifulTable

-----state.py----

-----utils.py----

from heapq import *

-----utils.py----

感谢任何帮助,谢谢!

【问题讨论】:

    标签: python python-2.7 pycharm python-import


    【解决方案1】:

    您需要从

    更改您的导入语句
    from graph import Vertex, Edge, UndirectedGraph
    from state import WorldState
    from utils import create_map_args, get_path_from_to
    

    到以下:

    from .graph import Vertex, Edge, UndirectedGraph
    from .state import WorldState
    from .utils import create_map_args, get_path_from_to
    

    当您引用同一目录中的模块时。点告诉 Python 导入来自同一目录/包中的另一个模块。如果你在 import 语句中去掉点,Python 认为你指的是全局包而不是本地包。更详细的解释请看 Python 官方的documentation

    编辑: 我忘了说你应该在目录中添加一个__init__.py 文件,以向 Python 表明该目录是一个 Python 包。

    【讨论】:

    • 嗨,感谢您的帮助,但我没有在这个项目中创建任何包。所以现在我收到一个错误,我在没有包的情况下使用点符号
    • 我更新了我的答案,包括将 __init__.py 模块添加到目录中,以向 Python 表明该目录是 Python 包。这应该可以解决您的新错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多