Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.
Hello,
I upgraded to the latest version of PYCharm PY-222.3345.131. I am running PyCharm on a Mac. The python version I am using is 3.9. Before the upgrade the same project I was able to run the application without any issues, but after the upgrade when I try to run the Flask app, I am getting the following error. Code has not changed from before the upgrade. When I run the command "python app/wsgi.py" from the terminal it works without any issues. The problem is when I click on the green run icon. Is there something special I need to add or change after the upgrade?
FLASK_APP = app/wsgi.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder /Users/jolim/Documents/PycharmProjects/ringotel_backend
/Users/jolim/Documents/VENV/ringotel_backend/bin/python -m flask run
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: offProcess finished with exit code 2
Please sign in to leave a comment.
Hi,
Most likely it's this known issue https://youtrack.jetbrains.com/issue/PY-55347/Flask-server-run-configuration-not-setting-FLASKAPP-correctly-in-PyCharm-20222
It will be fixed in the next update.
The workaround is:
python.use.targets.api
OK thanks, For right now I will just run it on the terminal.
Try to use the workaround, it does not work.
Need to us VS code instead.
And I am a paid user, need to use a free ide instead of paid pycharm.
Gordon do you get the same error after disabling Targets API and recreating the interpreter?
Is it reproducible in a new project?
Yes, I manually uninstall the old PyCharm version and download the latest one,
then disable python.use.targets.api
then recreating the interpreter with venv name venv2, pip install all requirements,
get this result:
When I create a new project via pycharm with Flask template, it is ok and it can run.
But with my current project, always errors. although I can run with app.py, but when I run with app.py, it is not in a development environment, without FLASK_ENV = development, it is useless for me.
Fortunately, I keep my vs code config, and run with vs code debugger works great. This also give me a chance to work with vs code, it is pretty good.
It seems the paid feature of PyCharm also appear in vs code.
Could you share a screenshot of the run configuration you are using?
sure, attached please find the screenshot. You can see I am using venv3 folder as my venv, which created from scratch.
Try pointing it to the app.py file in the Script path field. Has it helped?
I am seeing a similar issue, my previous flask configuration was working perfectly well and now the app fails to deploy with a message that says the following:
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.
This is especially strange as the environmental variables (notably FLASK_APP) seem to be set on the previous lines. I'll note that I am using pipenv to build the virtualenv as well.
Side note: I am not the Gordon from above, I am another Gordon. What are the chances that two Gordons run into the same incredibly specific issue?
It seems you are facing this known issue https://youtrack.jetbrains.com/issue/PY-55347/Flask-server-run-configuration-not-setting-FLASKAPP-correctly-in-PyCharm-20222
Try the RC build https://youtrack.jetbrains.com/articles/PY-A-23004355?_gl=1*ikkyid*_ga*Njc4NzIxMTI4LjE2Mjg3NjEzNTc.*_ga_9J976DJZ68*MTY2MDcxODQxNi4yNzIuMS4xNjYwNzE4OTUyLjUzLjAuMA..&_ga=2.185191979.340134403.1660328269-678721128.1628761357
Let me know if the problem reproduces in the RC.