FLASK_APP variable don't work even it's in .env - python

I have installed python-dotenv==0.6.5
And I put FLASK_APP in .env file
DEBUG=True
FLASK_ENV=development
FLASK_APP=flasky.py
But it shows this error when I run flask run
Usage: flask run [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/
But if I export FLASK_APP=flasky.py , it works well.
I also tried to put FLASK_APP=flasky.py in .flaskenv file, but I didn't work :(
All other env variables are loaded well. Thanks

The problem was FLASK version.
I updated it using pip install --upgrade Flask
Thanks!

Related

Setting flask environment variables in a shell script

all
As flask-script document said they are no longer to maintain it,
thus I am trying to use flask cli (Command Line Interface) to activate my flask app.
When I run the following flask cli commands one by one at my terminal, they worked fine for me.
$ export PYTHONPATH=$PWD:flask_api/
$ export FLASK_APP=flask_api/app
$ export FLASK_RUN_PORT=8000
$ flask run
The problem is that it seems not that efficient every time I tried to start my flask app.
so I came up with writing them into a shell scripts (that said run_flask.sh)
the content in run_flask.sh is:
export PYTHONPATH=$PWD:flask_api/
export FLASK_APP=flask_api/app
export FLASK_RUN_PORT=8000
flask run
and I simply run source run_flask.sh
and the result showed and did not start to run flask:
"* Serving Flask app "flask_api/app
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
".ror: Could not import "src.flask_api.app
I am not sure what's happening,
could any one tell me why flask could not import my flask app while I tried to run it with shell scripts? Great Thanks!
ps. I declare flask app instance in flask_api/base_api/init.py,
and app.py is in flask_api/ folder, app.py would import the app instance from base_api
(I used CentOS7 as the OS)

Problem with flask using Python 3.7.6 with vscode on mac

I have a weird problem with some code I want to run. The code itself should not be the problem since it is downloaded from a Udemy class and not modified:
# coding=utf-8
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/")
def hello():
items = ["Apfel", "Birne", "Banane"]
return render_template("start.html", name="Max Mustermann", items=items)
#app.route("/test")
def test():
name = request.args.get("name")
return render_template("test.html", name=name)
I found online that, to start the emulated webserver(?) I have to rund the following temrinal commands before I can see the output:
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py run flask
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py run flask
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py
(base) Christophs-MBP:13-23 chris$ run flask
bash: run: command not found
No reaction to my terminal commands
Basically there is no reaction to the command to start the server(?).It should reply with "Running on 127.0.0.1:5000" as soon as I've run the command once.
If I go to my browser, there is no page when I address http://127.0.0.1:5000. What am I doing wrong? I am pretty new to Python and an absolute rookie regarding the terminal. Not sure if I broke something there, since trying to install pyenv to manage my Python installs better as recommended by a friend does not work either (I cannot update the SDK headers as described on RealPython
What are the export statements?
On Mac, export key=value creates a new (or updates an existing one) environment variable - the tutorial most likely simply asked you to provide one where key is FLASK_APP and value is a path to your app.
To verify it's been saved correctly, you can list the variables by just typing export in the terminal and finding out what's inside each of the environment variables on your system (if you want to only view FLASK_APP you can type export | grep FLASK_APP).
Why do you need FLASK_APP?
When you call flask run in your terminal, you will see the following message:
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py"
module was not found in the current directory.
I presume your file is called run.py, therefore none of the conditions are met. You could rename run.py to app.py and simply type flask run in the terminal, but you can also type export FLASK_APP=<path-to-run.py>. It seems the tutorial author decided to do the latter. Keep in mind that if you rename your file to app.py you will need to run flask run within the directory that file lives in. You can change directory in the terminal using cd command.
Why do you get bash: run: command not found?
bash is a language running inside your terminal, and it only knows of a few commands - it is not aware of any run commands. It does however know about flask command once you have installed it on your machine. Within the command's output there is a part which includes a run command:
Commands:
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.
Therefore, what you want to do is type flask run instead of just run in your terminal.

Flask run isn't running and gives me error

im trying to run flask and when i run
flask run
I get this error
(snoop) ether#ether:~/Documents/loginsystem$ flask run
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
I have done
Run command: set FLASK_APP=main.py
Run command: set FLASK_DEBUG=1
Run command: flask run
but that doesn't change anything on the error
Im using this tutorial for a login system
https://codeshack.io/login-system-python-flask-mysql/
Can you try exporting the variable instead of just setting it ?
export FLASK_APP=main.py
and not
set FLASK_APP=main.py

How to run Flask app as a package in PyCharm

I was following this documentation on directory management for Flask projects. Now, I'm trying to run my flask application from PyCharm. I have added the below mentioned Environment Variables in Edit Configurations...:
FLASK_DEBUG=true
FLASK_APP=<absolute-path-to-root-directory-of-application>
I add the Script as flask run
The output running this configuration is this:
../red-flask/venv/bin/python "flask run"
../red-flask/venv/bin/python: can't open file 'flask run': [Errno 2] No such file or directory
Process finished with exit code 2
My project directory looks like:
/flask_app
setup.py
/flask_app
__init__.py
views.py
/static
style.css
/templates
layout.html
index.html
login.html
...
I am unable to figure out how to make this work, any help is appreciated.
This is documented in the development build of the docs.
You need to point to the location of the flask command.
Script: /path/to/env/bin/flask
Script parameters: run
Until 1.0 comes out, you need to point FLASK_APP at __init__.py if you don't install your package in your env.
Environment variables: FLASK_APP /path/to/flask_app/__init__.py
Preferably, install the package in the env and point to it using the import name.
From the terminal, in the virtualenv: pip install -e .
Environment variables: FLASK_APP flask_app

Flask-Migrate command 'flask db init' can't find app file

Firstly, I'm following the Python Flask tutorial posted here: https://scotch.io/tutorials/build-a-crud-web-app-with-python-and-flask-part-one.
Everything was working smoothly up to the 'Migration' section where executing:
$ flask db init
... failed with the following error:
Usage: flask db init [OPTIONS]
Error: The file/path provided (run) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
I know the $FLASK_APP env variable is set because this command executes fine:
$ flask run
Can anyone suggest why this executes fine when running the app, but not when trying to create the migration repository?
The closest I can find elsewhere on the subject is here: Flask can't find app file, but pre-pending with python -m isn't working in either case for me here.
In my case I had to do
python3 -m flask db init
Without mentioning python3 -m, it shows
zsh: command not found: flask
The command flask run started the app successfully.
The command flask db init failed with the error reported in the question.
So I tried python run.py and this failed to start the app by reporting an unmet dependency in models.py which was in fact a typo in an import. Fixing the typo and rerunning python run.py was successful.
Then I tried flask run again, still fine. Then flask db init... finally success.
It appears the error reported that it could not find run.py is either misleading or masking the true root cause of why it could execute.
In my case my app name was different, and in the directory, I had two flask apps app.py and app_async.py
And I was also getting the same migrate key error, so this is how I solved it:
FLASK_APP=app_async.py flask db init
Here I mentioned the app name, and then ran the command.
This worked for me:
pip install flask-migrate --upgrade
I had same problem occuring,after sometime i noticed that the set FLASK_APP=my_app,but i had a function handling creaion and initializing app so i changed set FLASK_APP=my_app:crate_flask_app,finally it's working for me.
for example:In my_app/init.py
migrate=Migrate()
db=Sqlalchemy()
defcreate_flask_app():
app=Flask(__name__)
db.init_app(app)
migrate.init_app(app,db)
return app
solution for windows is while declaring FLASK_APP environmental variable if any function is part of creating app then the function should be given after the colon i,e:set FLASK_APP:create_flask_app

Categories