[英]Python Package Import Error--Python Doesn't Recognize Package
Recently, in attempting to import a module I'd written, I have been coming across errors that ostensibly should not happen. 最近,在尝试导入我编写的模块时,我遇到了表面上不应该发生的错误。 Here is the idea, I'm writing in my
main.py
file, and my hierarchy looks like this: 这是我的主意,我正在写我的
main.py
文件,我的层次结构如下所示:
starsearch/
main.py
parser/
__init__.py
parse.py
the __ init __.py
file in parser/ is empty, but when I try, in my program, to: 解析器/中的
__ init __.py
文件为空,但是当我在我的程序中尝试执行以下操作时:
import parser
it returns an AttributeError
. 它返回一个
AttributeError
。 This happens when I call the function inside of parse.py
, called getstar()
. 当我调用
parse.py
的函数getstar()
称为getstar()
时会发生这种情况。 or 要么
from parser import parse
it returns an ImportError
. 它返回一个
ImportError
。
So my Python doesn't recognize that parse.py
exists? 所以我的Python不承认
parse.py
存在? I have done a bit of research, and having an __ init __.py
file that's empty should do the trick, but I'm stumped. 我已经做了一些研究,并且有一个空的
__ init __.py
文件应该可以解决这个问题,但我__ init __.py
。
parser is a name of a build-in module in python. parser是python中内置模块的名称。 when you write
当你写作
import parser
You import the built-in module. 您导入内置模块。 Since that module does not contain the getstar() function or a parse module, you get either AttributeError or ImportError
由于该模块不包含getstar()函数或parse模块,因此您将获得AttributeError或ImportError
Try changing the name of the "parser" directory to anything else and it should work. 尝试将“解析器”目录的名称更改为其他任何内容,它应该可以正常工作。 The empty init .py file is not needed
不需要空的init .py文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.