Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.

Answered

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: off

Process finished with exit code 2

0
11 comments

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:

  1. go to  Help | Find Action | Registry
  2. disable python.use.targets.api
  3. recreate the interpreter from scratch
0

OK thanks,  For right now I will just run it on the terminal.

0

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.

0

Gordon do you get the same error after disabling Targets API and recreating the interpreter?

Is it reproducible in a new project?

0

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: 

 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
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 'app:name' to specify one.

Process finished with exit code 2

0

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.

0

Could you share a screenshot of the run configuration you are using?

0

sure, attached please find the screenshot. You can see I am using venv3 folder as my venv, which created from scratch.

0

Try pointing it to the app.py file in the Script path field. Has it helped?

0

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?

0

Please sign in to leave a comment.