![]() |
flask.cli.NoAppException: Could not import 'app' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: flask.cli.NoAppException: Could not import 'app' (/thread-38719.html) |
flask.cli.NoAppException: Could not import 'app' - kazu755 - Nov-16-2022 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 </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. 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 !! RE: flask.cli.NoAppException: Could not import 'app' - SpongeB0B - Dec-28-2022 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.x/quickstart/#rendering-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.pyCheer, RE: flask.cli.NoAppException: Could not import 'app' - Larz60+ - Dec-28-2022 A very well written Flask tutorial: The Flask Mega Tutorial RE: flask.cli.NoAppException: Could not import 'app' - SpongeB0B - Feb-19-2023 (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).. RE: flask.cli.NoAppException: Could not import 'app' - Larz60+ - Feb-19-2023 Well, I used it to get started, and was impressed with the results. I should have stated that it was my opinion only. |