user1634254
user1634254

Reputation: 139

Flask: Reach app from subfolders

I have a hierarchy like this

Project/
|-- app/
|-- folder1/
|   |-- code1.py
|   |-- __init__.py
|-- __init__.py

I am trying from code1.py to reach my "app" Flask variable defined such as app = Flask(__name__) in __init__.py located in app folder

I can't seem to import my app variable, I have tried the following-

from Project.app import app
from .. import app
from ..app import app
from ...app import app

When I tried the Project.app import app I get import issues trying to import code1 (which contains a blueprint I am registering in __init__.py where app is defined.

Upvotes: 0

Views: 1521

Answers (1)

Kostas Pelelis
Kostas Pelelis

Reputation: 1342

Lets suppose that your code runs within the Project folder. That means that the current module directory is 'Project'. That means you can access all subfolders as long as they are treated as modules(the have a __init__.py file).

Project/
|-- run.py <
|-- app/
|-- folder1/
|   |-- code1.py
|   |-- __init__.py
|-- __init__.py

In run.py all of the following are valid

import folder1
import .
from folder1 import code1
from folder1.code1 import <submodule>

If your app is in folder1/__init__.py

from folder1 import app

Upvotes: 2

Related Questions