1

I have created a module, it's sitting in its own folder with an __init__.py and four files that contain my classes.

When doing from MyPackage import * I'm getting the modules that I have written into the __all__ statement in my __init__.py just as expected.

When doing from MyPackage import ModuleX I can import any module individually just fine.

When doing import MyPackage and then say dir(MyPackage) however, all I get is this:

['__all__',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__']

My modules aren't shown and I can't access them using MyPackage.ModuleX either.

The only thing I have written into my __init__.py is the __all__ = [ModuleX] statement.

Why does the last statement not see my modules? Do I have to set some more configuration?

1 Answer 1

1

__all__ determines what names are exported from that module. However in order to export them you would need to import them in the first place, which you haven't.

Sign up to request clarification or add additional context in comments.

1 Comment

So I need to import all modules that are part of the package into my __init__.py to use them with the import MyPackage command even though they can be readily imported with the from keyword?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.