Control module imports

gui commited 6 years ago · 🐍 Python

Let's imagine following structure

|_ A
|  |_
|  |_
|_ B
Code structure


This way, if you need to use both classes in , you will need to import like this:

from A.ClassA import ClassA
from B.ClassB import ClassB

Importing name

Which can get redundant, so in A we will do the following:

from .ClassA import ClassA
__all__ = ["ClassA"]

Now we can access this way:

from A import ClassA

Simple and direct to the point, you can repeat same for ClassB in its respective

Customizing imported name

Note that __all__ should always be a list of strings with names that you've imported. If I would want to change imported name, I could:


from .ClassA import ClassA as CustomA
__all__ = ["CustomA"]

from A import CustomA
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket