Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python module structure for classes

I'm currently working on a project that I am structuring in the following manner:

.
├── example_app
│  ├── __init__.py
│  ├── app.py
│  ├── requirements.txt
│  └── classes
│  ├── ExampleClass1.py
│  ├── ExampleClass2.py
│  ├── __init__.py
└── tests
 └── test_app.py

Inside of classes/__init__.py I have defined a base class for all of the other "ExampleClasses" to inherit from. Then, inside of app.py I need to import all of these classes. However, this becomes very verbose, as I have to type:

from example_app.classes.ExampleClass1 import ExampleClass1
from example_app.classes.ExampleClass2 import ExampleClass2
...

Is there a way to structure this in a better way? Ideally I would like to keep each class in a separate file as they are not really similar to each other. I thought of importing all the classes inside of classes/__init.py but that does not seem right.

Note that although I only pictured 2 example classes there could be several (tens) of them so importing by hand is quite cumbersome and brittle.

Answer*

Draft saved
Draft discarded
Cancel

lang-py

AltStyle によって変換されたページ (->オリジナル) /