Reputation: 1217
Suppose we have a tree like this:
└── my_project
├── A.py
├── __init__.py
├── my_apps
├── app_framework.py
├── app1.py
├── app2.py
├── __init__.py
Inside the folder my_apps
, there is a general class defined in app_framework.py
. The rest of the files all defines their own child class based on it.
The files would look like:
app_framework.py
:
Class App():
...
app1.py
:
from app_framework import App
Class MyApp1(APP):
...
...
app2.py
:
from app_framework import App
Class MyApp2(APP):
...
...
So, in my project folder, I want to use
from my_apps import MyApp1, MyApp2
But I got two errors:
First is ModuleNotFoundError: No module named app_framework
. I partially fix it by changing from app_framework import App
to from .app_framework import App
ImportError: cannot import name 'MyApp1' from 'my_apps'
I can use from my_apps.app1 import MyApp1, MyApp2
, but I would prefer from my_apps import MyApp1, MyApp2
, which looks more concise. How to do it?
Upvotes: 0
Views: 51
Reputation: 21893
Create an __init__.py
file in my_apps
and import the required classes into that file:
# my_project/my_apps/__init__.py
from .app1 import MyApp1
from .app2 import MyApp2
Then in your A.py
you can do:
from .my_apps import MyApp1, MyApp2
Upvotes: 3