-1

After setting the environment variables FLASK_ENV and FLASK_APP running flask run will give me this error: enter image description here

The snippet shows the command promt. It says that environment is production and that I didn't provide the FLASK_APP environment variable, even though I typed them in. Did I miss something or can someone explain why this error occurs?

DLO
  • 914
  • 1
  • 13
  • 30
  • 3
    Your issue is very simply a result of bad syntax. Change from `set FLASK_ENV = development`, and `set FLASK_APP = src/app.py`, to `Set "FLASK_ENV=development"`, and `Set "FLASK_APP=src/app.py"`, respectively. The main problem is that unlike in many other languages, spaces either side of the `=` are included as part of both the variable name and the value, This means that you've created, for instance, a variable `FLASK_APP`, with a value of `src/app.py`. _The doublequotes are not included in either the variable name, or value, but provide hidden or poison character protection_. – Compo Oct 31 '20 at 17:58
  • 1
    David Lo, instead of simply accepting an answer which only part uses the correct syntax, the least you could have done was upvote, or better, respond to, my above comment, thanking me for providing the correct syntax, complete with expalnatory text. A responsible questioner, would possibly direct the author of the accepted answer, to view my comment, and adjust their answer to match, or improve upon, it. – Compo Oct 31 '20 at 20:05

3 Answers3

2

i suggest you this good read https://www.twilio.com/blog/how-run-flask-application on how to run Flask Application (there are 2 approaches)

i also recommend you reading this topic https://flask.palletsprojects.com/en/1.1.x/cli/?highlight=flaskenv#environment-variables-from-dotenv

the better approach is to create .flaskenv file in the root of your project where you set your environment variables like so :

in /.flaskenv file

FLASK_APP=myflaskproject:create_app()
FLASK_ENV=development
FLASK_DEBUG=0

# FLASK_RUN_EXTRA_FILES=
# FLASK_RUN_HOST=
# FLASK_RUN_PORT=8080
# FLASK_RUN_CERT=
# FLASK_RUN_KEY=

in FLASK_APP you call your app but usually it's recommended to use "Application Factory" pattern, see https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/

then

(.venv) flask run

don't forget to install python-dotenv

cizario
  • 3,995
  • 3
  • 13
  • 27
1

At least on Linux you cannot have spaces around the equal sign. Maybe on Windows this is the same.

In the official Flask documentation you can read:

set FLASK_APP=hello

https://flask.palletsprojects.com/en/1.1.x/cli/

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37
  • This suggestion fixed my issue. Thanks. @Compo left a more explanatory answer in the comment which would be good to read into following your answer. – DLO Nov 01 '20 at 23:14
1
set flask<space>=<space>src/app.py

As you can see you are NOT setting flask but flask<space>. Flask may accept that path but it is an illegal windows path.