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…?
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.
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:
- use open in terminal of your compiler
then type
pip install virtualenv
virtualenv env
envscriptsactivate.bat
pip install flask flask-sqlalchemy
python hello.py
Then your web server will be given.
copy paste in google chrome.
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.
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
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.
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
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…?
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.
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:
- use open in terminal of your compiler
then type
pip install virtualenv
virtualenv env
envscriptsactivate.bat
pip install flask flask-sqlalchemy
python hello.py
Then your web server will be given.
copy paste in google chrome.
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.
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
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.
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