Python Forum

Full Version: flask.cli.NoAppException: Could not import 'app'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Background
I am currently making flask-based application with Python but fatal error shown in the title occurs. I searched around the net and found similar questions but there seems be no way to resolve. So I post this issue here to receive comments and advise.

What I want to do
I want to be able to launch flask-based program.

Environment
Windows10 64bit
Python v3.10.5 (Installer was downloaded from Python.org/downloads/windows/)
flask v2.2.2
I edit all code and run program on the same computer.

Configuration
In the user environmental variables in the environment settings window(Control Panel-> Detail Information-> Detail settings of the system-> System Property-> Environmental Variables),following variables are registered:

Variable name = FLASK_APP, Value = app.py
Variable name = FLASK_DEBUG, Value = 0

Code
content inside the endpoint '/main' is almost omitted. This is because endpoint 'main' is included inside the endpoint '/'.

# -*- coding:utf-8 -*-
from flask import Flask, render_template, url_for

app = Flask(__name__)

@app.route("/",methods=["POST","GET"])
def index():
    username = "hogehoge"

    BASE_HTML="""
    <!DOCTYPE html>
    <html lang="ja">
    <head>
        <meta charset='utf-8'>
        <title>File Transfer Program</title>
    </head>
    <body>
    
    <h2 style="width:98%;
               background-color:LIGHTBLUE;
               padding-top:10px;
               padding-bottom:10px;
               padding-left:20px;
               margin:0 auto;">File Transfer Program</h2>
  
    <br>
    
    <div style='margin:0 auto;'>
            <div style='width:400px; margin:0 auto;background-color:#f1c93c;'>
            <table>
                <form action='{{ url_for("main") }}' method='POST' style='margin:10px;'>
                    <tr style='height:30px;'>
                        <td style='width:100px;text-align:right;'>username&nbsp;</td>
                        <td style='width:250px;'><input type='text' name='username' size=30 value='"""+username+"""'></td>
                    </tr>
                    <tr>
                        <td style='width:100px;text-align:right;'>password</td>
                        <td style='width:250px;'><input type='password' name='password' size=30></td>
                    </tr>
                    <tr>
                        <td colspan=2 style='text-align:center;'>
                            <input type='submit' value='SUBMIT'>
                        </td>
                    </tr>
                </form>
            </table>
        </div>
    </div>
    
    </body>
    </html>
    """

    os.chdir("c:\\users\\"+username+"\\documents\\python.source\\templates")
    with open("index.html",mode="w",encoding="utf-8") as f:
        f.write(BASE_HTML)
    return render_template("index.html")

@app.route("/main",methods=["GET","POST"])
def main():
    global baseFolder
    username = request.form["username"]
    password = request.form["password"]

    return "<h2>Hello</h2>"

if __name__ == '__main__':
    app.run(debug=True)
Error
This is an output on command prompt.

Error:
C:\\users\\username\\documents > flask run app= <Flask 'app'> * Serving Flask app 'app.py' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit * Restarting with stat Traceback (most recent call last): File "C:\py64-3105\lib\site-packages\flask\cli.py", line 897, in run_command app = info.load_app() File "C:\py64-3105\lib\site-packages\flask\cli.py", line 308, in load_app app = locate_app(import_name, name) File "C:\py64-3105\lib\site-packages\flask\cli.py", line 228, in locate_app raise NoAppException(f"Could not import {module_name!r}.") from None flask.cli.NoAppException: Could not import 'app'. * Debugger is active! * Debugger PIN: 907-919-858
What is the solution to resolve this issue ?
When I start the program by typing flask run, flask replies 'Could not import app'. What is the solution to resolve this error ? I want to hear comments and advices !! Please help me !!
Hi @kazu755 ,

No problem we will most certainly make your project run :)

I see a lot of bad Flask examples/tutorial on the WWW especially the launching part.

If you start with Flask I will recommend you to start from scratch with the official documentation
https://flask.palletsprojects.com/en/2.2.x/quickstart/

---
You should not put the HTML code in your .py Python/Flask file
Line 10 to 56. instead you want to use template file -->
https://flask.palletsprojects.com/en/2.2...-templates
You might find better documentation about Jinja "templating" (it's damn useful !)

---
You don't launch by Flask file.py

but trough Python

so either run.py (if python is associated with .py file)
or something like

cd C:\users\username\documents 
C:\something\Python\Python3.11.exe run.py
Cheer,
A very well written Flask tutorial: The Flask Mega Tutorial
(Dec-28-2022, 09:54 AM)Larz60+ Wrote: [ -> ]A very well written Flask tutorial: The Flask Mega Tutorial

I've quickly reviewed it, I'm not a big fan. He use a lot of heavy external library, mix the subjects (Javascript chapter)..
Well, I used it to get started, and was impressed with the results.
I should have stated that it was my opinion only.