'app.shell_context_processor decorator does not register the function as a shell context function
I created the following function in a microblog.py
file in my ~/Programing/Rasa/myflaskapp/app
folder. It creates a shell context that adds a database instance and models to the shell session:
from app import app, db
from app.models import User, Post
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Post': Post}
The app.shell_context_processor
decoder registers the function as a shell context function. But when the flask shell command is executed, in ~/Programing/Rasa/myflaskapp/
it does not invoke this function and records the elements returned by it in the shell session as expected.
So I get this:
(MyFlaskAppEnv) mike@mike-thinks:~/Programing/Rasa/myflaskapp$ flask shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
App: app [production]
Instance: /home/mike/Programing/Rasa/myflaskapp/instance
>>> db
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'db' is not defined
Rather than :
(venv) $ flask shell
>>> db
<SQLAlchemy engine=sqlite:////Users/migu7781/Documents/dev/flask/microblog2/app.db>
Update : I tried to check if the function was well saved
But it seems not :
>>> print(app.shell_context_processors[0]())
Traceback (most recent call last):
File "<console>", line 1, in <module>
IndexError: list index out of range
I changed microblog.py
only with importing app
and db
from app import app, db
@app.shell_context_processor
def make_shell_context():
return {'db': db}
I tried to put microblog.py
it in the app
folder or even remove it, it's always the same error : I am not able to register functions as a shell context function. In the same time when I call for >>> app
in the Flask context I do have an answer.
Solution 1:[1]
I told Flask how to import the application, by setting the FLASK_APP environment variable:
export FLASK_APP=microblog.py
It seems to make it !
Solution 2:[2]
If you use CMD then: set FLASK_APP=microblog.py
If you use PowerShell then: $env:FLASK_APP = '.\microblog.py'
If you use Bash(Linux) then: export FLASK_APP=microblog.py
The problem is with the variable FLASK_APP
Solution 3:[3]
I had the same issue. I had to re-export the FLASK_APP variable.
Solution 4:[4]
In my case the problem was that i used app
variable in some util function, so i create it and pass directly to func, eg.
# microblog.py
from app import create_app, func
func(create_app())
Because of that there were no app
instance. When you run flask cli commands, Flask
tries to locate app first by variable name. It checks FLASK_APP
file for "app", "application" variable name that is instance of Flask
. If it's not found then it looks for app factory function named "create_app", "make_app" (that's why it runs its twice since app instance wasn't found). So all i had to do was to keep app
in file.
# microblog.py
from app import create_app, func
app = create_app()
func(app)
Solution 5:[5]
Chances are you have the app name and project as the same. I changed my FLASK_APP name to start.py and now context is working correct
Solution 6:[6]
in Windows PyCharm (venv) i used:
set FLASK_APP=microblog.py
And it works!) Instead of Linux:
export FLASK_APP=microblog.py
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alice Antoine |
Solution 2 | marc_s |
Solution 3 | TallPaul |
Solution 4 | Karolius |
Solution 5 | solly989 |
Solution 6 | ????? ???????? |