7

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.

Alice Antoine
  • 411
  • 6
  • 17
  • 1
    you can try `print(app.shell_context_processors[0]())` to check if the function is well saved. Otherwise could you try with a simple file and simple variables, without other modules imports ? – PRMoureu Jul 14 '18 at 11:51
  • @PRMoureu Thank you for your comment! I tried your ideas and updated my question! It seems that I am not able to register functions as a shell context function – Alice Antoine Jul 15 '18 at 13:55
  • not sure about that, but can you try to call the variable inside an app context: `with app.app_context(): ...` – PRMoureu Jul 15 '18 at 15:59
  • I added a line `with app.app_context(): return {'db': db, 'User': User, 'Post': Post}` and tried to call for db with this file `microblog.py` both in `/app` directory and home directory but I still got the error. It's like the file isn't recognized – Alice Antoine Jul 16 '18 at 11:20
  • I have run into the same error following the microblog.py tutorial. Was there ever a resolution? – Non-Contradiction Nov 08 '19 at 19:17

6 Answers6

10

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 !

Alice Antoine
  • 411
  • 6
  • 17
  • yep now env | grep FLASK says something whereas before it did not. not sure how FLASK_APP environment variable disappeared as I had set it before. – user391339 Oct 11 '19 at 07:43
  • "Since environment variables aren't remembered across terminal sessions, you may find tedious to always have to set the FLASK_APP environment variable when you open a new terminal window. Starting with version 1.0, Flask allows you to register environment variables that you want to be automatically imported when you run the flask command. To use this option you have to install the python-dotenv package:" Then create a .flaskenv file in the top-level directory that contains FLASK_APP=microblog.py – user391339 Oct 11 '19 at 07:47
3

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dmitry
  • 31
  • 2
2

I had the same issue. I had to re-export the FLASK_APP variable.

TallPaul
  • 31
  • 3
0

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)
Karolius
  • 543
  • 1
  • 6
  • 12
0

Link to answer

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

solly989
  • 33
  • 4
0

in Windows PyCharm (venv) i used:

set FLASK_APP=microblog.py

And it works!) Instead of Linux:

export FLASK_APP=microblog.py