Python 绝对导入模块失败
我有一个如下所示的项目:
my_project/
__init__.py -- empty
run.py
datacheck/
__init__.py -- empty
datacheck.py -- containing class DataCheck(object)
config.py -- containing BusinessConfig(object)
business.py -- containing class BusinessCheck(DataCheck)
我的 PYTHONPATH
配置为其中包含 /my_project 。
在 run.py 中,我有以下代码:
from datacheck.business import BusinessCheck
business = BusinessCheck()
business.check_data()
在business.py 中,我有以下失败的导入:
from datacheck.config import BusinessConfig
from datacheck.datacheck import DataCheck
像 from .config import BusinessConfig
这样的相对导入有效 - 但是我已经在许多线程中阅读过绝对进口是首选。
为了进行简单的测试,我还创建了以下内容:
myproject/
__init__.py -- empty
run_test.py
test/
__init__.py -- empty
test1.py -- containing class Test1(object)
test2.py -- containing class Test2(Test1)
run_test.py 导入并运行 Test2
类,这没有失败。
这让我有点惊讶,我不明白为什么我在 datacheck 中的绝对导入不起作用 - 谁能解释一下?
I have a project which looks like this:
my_project/
__init__.py -- empty
run.py
datacheck/
__init__.py -- empty
datacheck.py -- containing class DataCheck(object)
config.py -- containing BusinessConfig(object)
business.py -- containing class BusinessCheck(DataCheck)
My PYTHONPATH
is configured to have /my_project in it.
In run.py, I have the following code:
from datacheck.business import BusinessCheck
business = BusinessCheck()
business.check_data()
In business.py, I have the following imports that fail:
from datacheck.config import BusinessConfig
from datacheck.datacheck import DataCheck
A relative import like from .config import BusinessConfig
works - however I've read in numerous threads that an absolute import is preferred.
To do a simple test, I also created the following:
myproject/
__init__.py -- empty
run_test.py
test/
__init__.py -- empty
test1.py -- containing class Test1(object)
test2.py -- containing class Test2(Test1)
run_test.py imports and runs the Test2
class, this didn't fail.
It left me a bit flabbergasted, I don't understand why my absolute imports in datacheck are not working - can anyone explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的模块可以用作
__main__
,您应该更喜欢绝对导入,如 文档。如果没有,相对进口就可以了。这些导入失败,因为您的包
datacheck
包含模块datacheck
(相同名称)。当查找名称时,Python 会首先隐式地查找包内部。在那里,它找到了模块datacheck
。但是,该模块不包含名为config
的任何内容,因此导入失败。如果您想使用绝对导入,请将模块
datacheck
中的所有内容移至包的__init__.py
中,或重命名它。You should prefer absolute imports if your module can be used as
__main__
, as explained in the documentation. If not, relative imports are fine.These imports fail, because your package
datacheck
contains a moduledatacheck
(same name). When looking up the name, Python implicitly looks inside the package first. There, it finds the moduledatacheck
. This module, however, does not contain anything with the nameconfig
, so the import fails.If you want to use absolute imports, move all the stuff from the module
datacheck
into the__init__.py
of the package, or rename it.我知道这是很多年后的事了,但为了其他人在这里搜索,我能够用这段代码解决类似的问题:
之后,绝对导入在 Python 2.6 和 2.7 中工作得很好。
I know this is many years later, but for the sake of others searching here, I was able to resolve a similar problem with this bit of code:
After that, absolute imports worked fine in Python 2.6 and 2.7.