如何在Python 3中使这些相对导入工作? [英] How do I make these relative imports work in Python 3?
问题描述
我的目录结构如下所示:
I have a directory structure that looks like this:
project/
__init__.py
foo/
__init.py__
first.py
second.py
third.py
plum.py
在项目/ foo / __ init __。py
我从 first.py导入类
, second.py
和 third.py
并将它们放入 __ all __
。
In project/foo/__init__.py
I import classes from first.py
, second.py
and third.py
and put them in __all__
.
中有一个类first.py
名为 WonderfulThing
我想在 second.py
中使用,并希望通过导入 *进行导入/ code>来自
foo
。 (这个问题的范围超出了我为什么要这样做,假设我有充分的理由。)
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.)
在秒。 py
我尝试过来自.foo import * ,来自foo import *
和来自。 import *
并且在这些情况下都没有导入 WonderfulThing
。我还尝试了来自..foo import * 的,这会引发错误尝试相对导入超出toplevel包。
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".
我已经阅读了文档和PEP,我无法弄清楚如何使这项工作。任何帮助将不胜感激。
I've read the docs and the PEP, and I can't work out how to make this work. Any assistance would be appreciated.
澄清/编辑:似乎我可能误解了 __ all __ <的方式/ code>在包中工作。我使用它和模块一样,
Clarification/ 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" ]
但再次查看文档似乎建议 __ all __
只能在包中使用,以指定默认导入的模块的名称;似乎没有任何方法可以包含任何不是模块的东西。
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.
这是正确的吗?
编辑:非通配符导入失败(无法导入名称WonderfulThing
)。从中尝试。 import foo
失败,但 import foo
有效。不幸的是, dir(foo)
什么都没有显示。
A non-wildcard import failed (cannot import name WonderfulThing
). Trying from . import foo
failed, but import foo
works. Unfortunately, dir(foo)
shows nothing.
推荐答案
编辑:我确实误解了这个问题:没有 __所有__
并不仅限于模块。
I did misunderstand the question: No __all__
is not restricted to just modules.
一个问题是你想做什么相对进口。从project.foo import * 执行,这里没有任何问题。其次,对foo的
__ all __
限制不会阻止你从project.foo.first导入导入WonderfulThing
,或者只需从.first导入WonderfulThing
,这仍然是最好的方式。
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.
如果你真的想导入很多事情,最好从项目导入foo 做,然后使用
foo.WonderfulThing
而不是做 import *
,然后直接使用 WonderfulThing
。
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.
但是要回答您的直接问题,要从second.py中的 __ init __
文件导入,请执行以下操作:
However to answer your direct question, to import from the __init__
file in second.py you do this:
from . import WonderfulThing
或
from . import *
这篇关于如何在Python 3中使这些相对导入工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!