Caio Muniz
Caio Muniz

Reputation: 11

Running Flask on bash: Could not import "app"

I made this code on Flask:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "hi"

Even so, fi I try to run it with flask run it shows this message:

* Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not import "app".

What am I doing wrong?

Edit: App structure:

website/
  | - app.py

Upvotes: 1

Views: 9105

Answers (3)

Emma
Emma

Reputation: 1

If you do not provide the correct path, Flask will not be able to import your app. Check if you are inside the right folder with

pwd

You might see something like (I use MacOS so it might appear differently on your end if you use another OS)

/Users/your_username/Desktop/current_file

If you are not at the correct file, app, you can add in the path for that file, say the website file is inside your current_file. Then:

 export FLASK_APP=/Users/your_username/Desktop/current_file/website/app.py

Then do flask run . Hope this helped!

Upvotes: 0

Newton Akhur
Newton Akhur

Reputation: 1

If you have configured a virtualenv for your project, you have to activate the virtualenv with this command

>>>env\scripts\activate

Then make sure you are in the same directory where your app.py is located then use this commands

env>>>Set FLASK_APP=app.py 
env>>>Set FLASK_ENVIRONMENT=development
env>>>flask run

keep cracking **note if you are on linux use 'export' instead of 'set'

Upvotes: 0

Umar Hayat
Umar Hayat

Reputation: 4991

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "hi"


if __name__ == '__main__':
  app.run(host='0.0.0.0', port=30006, debug=True)

Run in the same directory where this code is placed.

python3 file_name.py

You can access it on

http://0.0.0.0:30006/

Working screenshot

Upvotes: 1

Related Questions