Why this architecture is not working?
/test
__init__.py
app.py
models.py
/subpackage
__init__.py
subpackage.py
Here's the example code:
app.py
from test.subpackage import hi_from_subpackage
hi_from_subpackage()
subpackage/subpackage.py
from test.models import models
def hi_from_subpackage():
print('Hi')
# I nee models here too
models()
the error is:
Traceback (most recent call last):
File "app.py", line 1, in <module>
from test.subpackage import hi_from_subpackage
**ModuleNotFoundError: No module named 'test.subpackage'**
What i'm doing wrong?
- Thanks in advance
You are getting confused with the import system of python, it happens. When you import subpackage, you want to import a module subpackage
from a package subpackage
so it should look like this:
from subpackage.subpackage import hi_from_subpackage
and when you are lower in the package hierarchy, you don't need to say witch package the module come from, it already "knows" it as it is in a higher hierarchy.
from models import models
Think about it like if you were writing core. Here models is in the scope of subpackage in a global variable way. And when you are standing in test package, you need to refer to a lower lever package by its name, as you do with a variable.
Try running pip install test
from your cmd if is Windows. With regard to the IDE you are using I would recommend vs-code to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.