简体   繁体   English

Python ImportError无法导入名称

[英]Python ImportError Cannot Import Name

In my working folder I have a file called exceptions.py , which contains only: 在我的工作文件夹中,有一个名为exceptions.py的文件,其中仅包含:

class ParseException(Exception):
    pass

class QueryException(Exception):
    pass

In the same folder I have a file, lets call it my_script.py 在同一个文件夹中,我有一个文件,命名为my_script.py

In my_script.py , when I do from exceptions import ParseException I get an error: my_script.py中 ,当我from exceptions import ParseException ,出现错误:

ImportError: cannot import name ParseException ImportError:无法导入名称ParseException

I cannot figure out what's going on here. 我不知道这是怎么回事。 I've never seen this error before, and when I looked it up I mainly see problems with circular dependencies, but I don't have any here... 我以前从未见过此错误,当我查找该错误时,主要看到循环依赖问题,但这里没有任何问题...

There is the exceptions module in your Python library which doesn't have the methods/classes you're trying to import. Python库中有一个exceptions 模块 ,其中没有要尝试导入的方法/类。 Change your file name to something different and it'll work. 将您的文件名更改为其他名称,它将起作用。

Try the following yourself 自己尝试以下

>>> import exceptions 
# Works OK
>>> from exceptions import ParseException
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name ParseException
>>> from exceptions import ImportError # Works OK too

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM