Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 739
Ketan Ingale Avatar asked Sep 30 '18 19:09

Ketan Ingale


People also ask

What is FLASK_APP?

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. Unix Bash (Linux, Mac, etc. ): $ export FLASK_APP=hello $ flask run.

Could not locate flask application you did not provide the FLASK_APP environment variable?

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 error happens when flask run command is run before setting The FLASK_APP environment variable.

How do I set environment variables in flask?

Command-line: Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.

How do I run a flask server?

To run the app outside of the VS Code debugger, use the following steps from a terminal: Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .


4 Answers

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.
like image 115
Parth Sharma Avatar answered Oct 06 '22 22:10

Parth Sharma


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.

like image 26
Swift Avatar answered Oct 07 '22 00:10

Swift


use virtual environment. Follow these steps:

  1. use open in terminal of your compiler

then type

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

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

like image 35
preetam Satapathy Avatar answered Oct 07 '22 00:10

preetam Satapathy


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

like image 21
user14348674 Avatar answered Oct 06 '22 22:10

user14348674