Python 绝对导入模块失败

发布于 2024-12-15 21:13:09 字数 1212 浏览 0 评论 0原文

我有一个如下所示的项目:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-12-22 21:13:09

如果您的模块可以用作 __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 module datacheck (same name). When looking up the name, Python implicitly looks inside the package first. There, it finds the module datacheck. This module, however, does not contain anything with the name config, 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.

痞味浪人 2024-12-22 21:13:09

我知道这是很多年后的事了,但为了其他人在这里搜索,我能够用这段代码解决类似的问题:

from __future__ import absolute_import

之后,绝对导入在 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:

from __future__ import absolute_import

After that, absolute imports worked fine in Python 2.6 and 2.7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文