Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 . importWonderfulThing

or

from . import *

Post a Comment for "How Do I Make These Relative Imports Work In Python 3?"