import python class into another folder in a package
I don't know about Flask specifically, but in Python you should generally include a __init__.py
in every folder and subfolder for them to be recognised as modules and/or packages.
In the diagram you provide, there is no __init__.py
in the app
folder. Try putting one and re-post if that resolves your problem.
If it doesn't work, try:
import osprint(os.getcwd())
If the current working directory isn't the app
directory, you'll have to work around this. One way would be:
import syssys.path.append('/path/to/app')import myapp.models
You can always see what directories are in your search path for modules by checking sys.path
.
Update: Sorry I just realized that maybe you' re trying to run connections.py
as a script. That would change you working directory to that of connections.py
. Try importing connections.py
from your main script with import models
inside connections.py
and see if you get an error.
Update (in reply to Edit 4): (I don't have enough reputation points to comment yet, so I reply here)
from models import dbdb.init_app(app)import myapp.routes
The problem in this piece of code is the import myapp.routes
. Change that to import routes
and it should work. As a rule of thumb, your main script's directory is your root directory. You can import any module located in your root directory from inside any other module (even modules inside a sub-folder!) with a simple import module
.