'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_Dog\scripts folder.
Then in command prompt, I wrote the following commands.
C:\Users\Ketan Ingale>set FLASK_APP=D:\Cat_vs_Dog\scripts\hello.py
C:\Users\Ketan Ingale>flask run
* Serving Flask app "D:\Cat_vs_Dog\scripts\hello.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...?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
use virtual environment. Follow these steps:
- use open in terminal of your compiler
then type
pip install virtualenv
virtualenv env
env\scripts\activate.bat
pip install flask flask-sqlalchemy
python hello.py
Then your web server will be given. copy paste in google chrome.
Solution 4:[4]
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 "env\Scripts\activate" instead "cd env\Scripts\activate".
I hope it will help to others
Solution 5:[5]
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
Solution 6:[6]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Swift |
Solution 2 | Parth Sharma |
Solution 3 | Ophir Carmi |
Solution 4 | user14348674 |
Solution 5 | Roman Halychanivskyi |
Solution 6 | lator |