Skip to content
Advertisement

Could not import “D”: FLASK_APP

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

I am new to Flask. I wrote this basic code and save it in hello.py in my D:Cat_vs_Dogscripts folder.

Then in command prompt, I wrote the following commands.

C:UsersKetan Ingale>set FLASK_APP=D:Cat_vs_Dogscriptshello.py

C:UsersKetan Ingale>flask run
 * Serving Flask app "D:Cat_vs_Dogscriptshello.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not import "D".

I am getting this error. What should I do…?

Advertisement

Answer

Flask v1.1.x

https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery

The above documentation link describes:

The flask command is installed by Flask, not your application; it must be told where to find your application in order to use it. The FLASK_APP environment variable is used to specify how to load the application.

Remove the directory and just use the name of the file and retry.

You might need to CD to the directory with your file in it also.

Flask 2.2.x

https://flask.palletsprojects.com/en/2.2.x/cli/#application-discovery

The documentation link above specifies:

The flask command is installed by Flask, not your application; it must be told where to find your application in order to use it. The --app option is used to specify how to load the application.

And also:

If –app is not set, the command will try to import “app” or “wsgi” (as a “.py” file, or package) and try to detect an application instance or factory.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement