How do I make these relative imports work in Python 3?

戏子无情 提交于 2019-12-01 11:12:10

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 *
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!