如何使这些相对导入在 Python 3 中工作?

发布于 2024-08-07 20:32:39 字数 1238 浏览 3 评论 0原文

我的目录结构如下所示:

project/
        __init__.py
        foo/
            __init.py__
            first.py
            second.py
            third.py
        plum.py

project/foo/__init__.py 中,我从 first.pysecond.py 导入类和 third.py 并将它们放入 __all__ 中。

first.py 中有一个名为 WonderfulThing 的类,我想在 second.py 中使用它,并希望通过导入 来导入它>* 来自 foo。 (这超出了这个问题的范围,为什么我想这样做,假设我有充分的理由。)

second.py 中,我尝试了 from .foo import *< /code>、from foo import *from . import * 并且在这些情况下都不会导入 WonderfulThing。我还尝试了 from ..foo import *,这会引发错误“尝试相对导入超出顶级包”。

我已阅读文档和 PEP,但我不知道如何进行这项工作。任何帮助将不胜感激。

澄清/编辑:看来我可能误解了__all__在包中的工作方式。我在模块中使用它的方式与在模块中相同,

from .first import WonderfulThing
__all__ = [ "WonderfulThing" ]

但是再次查看文档似乎表明 __all__ 只能在包中使用来指定默认情况下要导入的模块的名称;似乎没有任何方法可以包含非模块的任何内容。

这是正确的吗?

非通配符导入失败(无法导入名称 WonderfulThing)。尝试从 . import foo 失败,但 import foo 有效。不幸的是,dir(foo)没有显示任何内容。

I have a directory structure that looks like this:

project/
        __init__.py
        foo/
            __init.py__
            first.py
            second.py
            third.py
        plum.py

In project/foo/__init__.py I import classes from first.py, second.py and third.py and put them in __all__.

There's a class in first.py named WonderfulThing which I'd like to use in second.py, and want to import by importing * from foo. (It's outside of the scope of this question why I'd like to do so, assume I have a good reason.)

In second.py I've tried from .foo import *, from foo import * and from . import * and in none of these cases is WonderfulThing imported. I also tried from ..foo import *, which raises an error "Attempted relative import beyond toplevel package".

I've read the docs and the PEP, and I can't work out how to make this work. Any assistance would be appreciated.

Clarification/Edit: It seems like I may have been misunderstanding the way __all__ works in packages. I was using it the same as in modules,

from .first import WonderfulThing
__all__ = [ "WonderfulThing" ]

but looking at the docs again it seems to suggest that __all__ may only be used in packages to specify the names of modules to be imported by default; there doesn't seem to be any way to include anything that's not a module.

Is this correct?

A non-wildcard import failed (cannot import name WonderfulThing). Trying from . import foo failed, but import foo works. Unfortunately, dir(foo) shows nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

拥抱影子 2024-08-14 20:32:39

编辑:我确实误解了这个问题:不 __all__ 不仅限于模块。

一个问题是为什么要进行相对导入。在这里执行 from project.foo import * 没有任何问题。其次,对 foo 的 __all__ 限制不会阻止您执行 from project.foo.first import WonderfulThingfrom .first import WonderfulThing >,这仍然是最好的方法。

如果您确实想导入很多东西,最好是执行 from project import foo ,然后将这些东西与 foo.WonderfulThing 一起使用,而不是执行 < code>import * 然后直接使用WonderfulThing

但是,要回答您的直接问题,要从 secondary.py 中的 __init__ 文件导入,您可以执行以下操作:

from . import WonderfulThing

from . import *

Edit: I did misunderstand the question: No __all__ is not restricted to just modules.

One question is why you want to do a relative import. There is nothing wrong with doing from project.foo import *, here. Secondly, the __all__ restriction on foo won't prevent you from doing from project.foo.first import WonderfulThing, or just from .first import WonderfulThing, which still will be the best way.

And if you really want to import a a lot of things, it's probably best to do from project import foo, and then use the things with foo.WonderfulThing instead for doing an import * and then using WonderfulThing directly.

However to answer your direct question, to import from the __init__ file in second.py you do this:

from . import WonderfulThing

or

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