Could not import "D": FLASK_APP

Question:

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…?

Asked By: Ketan Ingale

||

Answers:

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.

Answered By: Swift

I recommend that you deploy a virtual environment on one disk and work with the application right there! and your file.py should be in this folder called “venv”.
https://flask.palletsprojects.com/en/1.1.x/installation/#install-virtualenv

use virtual environment.
Follow these steps:

  1. use open in terminal of your compiler

then type

  1. pip install virtualenv
  2. virtualenv env
  3. envscriptsactivate.bat
  4. pip install flask flask-sqlalchemy
  5. python hello.py

Then your web server will be given.
copy paste in google chrome.

Answered By: preetam Satapathy
from flask import Flask
app = Flask(__name__)

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

We can simply use app.run() to solve this kind of problem.

Answered By: Parth Sharma

when you came into the directory using command prompt where you installed virtual environment setup in windows.

Then don’t use ‘cd’ to change the directory to switch into ‘virtual environment’ folder

Example:

use "envScriptsactivate" instead "cd envScriptsactivate".

I hope it will help to others

Answered By: user14348674

If it shows an import error, it most likely means you didn’t set the environment variable for FLASK_APP properly.

If you’re on windows, use $env:FLASK_APP="nameofyourapp.py"

On Mac and Linux bash:
set FLASK_APP=nameofyourapp.py

If it still shows the error, make sure you’re in the powershell terminal not the Python one.

Answered By: lator

I advise you to keep the file in the same path/location where you are running the terminal

A good option to avoid import errors is to create a .flaskenv file and set the FLASK_APP variable there.

FLASK_APP=nameOfApp #without the extension

If your script is in a folder ‘src’ for example,

FLASK_APP=src.nameOfApp #src treated as a module
                        #in older versions of Python, you'll need 
                        #to add an __init__.py to the folder
Answered By: lator
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.