如何使这些相对导入在 Python 3 中工作?
我的目录结构如下所示:
project/
__init__.py
foo/
__init.py__
first.py
second.py
third.py
plum.py
在 project/foo/__init__.py
中,我从 first.py
、second.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我确实误解了这个问题:不
__all__
不仅限于模块。一个问题是为什么要进行相对导入。在这里执行
from project.foo import *
没有任何问题。其次,对 foo 的__all__
限制不会阻止您执行from project.foo.first import WonderfulThing
或from .first import WonderfulThing
>,这仍然是最好的方法。如果您确实想导入很多东西,最好是执行
from project import foo
,然后将这些东西与foo.WonderfulThing
一起使用,而不是执行 < code>import * 然后直接使用WonderfulThing
。但是,要回答您的直接问题,要从 secondary.py 中的 __init__ 文件导入,您可以执行以下操作:
或
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 doingfrom project.foo.first import WonderfulThing
, or justfrom .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 withfoo.WonderfulThing
instead for doing animport *
and then usingWonderfulThing
directly.However to answer your direct question, to import from the
__init__
file in second.py you do this:or