How do I solve "flask.cli.NoAppException: Could not import filename"? - python

I have a simple app.
app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return app.send_static_file('../../public/index.html')
if __name__== '__main__':
app.run(debug=True)
When I run it using flask run I get this error:
flask.cli.NoAppException: Could not import app
My FLASK_APP is set to {path-to-app}/app.py, and I am running the command from the folder that the file is in.
Can somebody help?

Your html file "index.html" needs to be in a folder called 'templates' which should be in the folder 'app.py' is also in.

Related

Flask: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

I am just a beginner in this api creating with python and I was trying to create an api.
But when I run the code, it gives me the error : 404
init.py
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
api = Api(app)
routes.py
from flask_restful import Resource
from src import api
class Smoke(Resource):
def get():
return {'message': 'Ok'}, 200
api.add_resource(Smoke, '/')
wsgi.py
from src import app
if __name__ == '__main__':
app.run()
The routes.py file is never called, so the route is never bound to the api.
Change your wsgi.py file to:
wsgi.py
from src import app, api
from src import Smoke
if __name__ == '__main__':
api.add_resource(Smoke, '/')
app.run()

Flask - 404 Not found : The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

I'm completely new to Flask. As a beginner I'm trying to print 'Hello World' on a web page. When run this Flask application, the browser throwing me a 404 error. It says The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
Here is my __init__.py :
from flask import Flask
app = Flask(__name__)
from app import app
My routes.py file:
from app import app
#app.route('/')
#app.route('/index')
def index():
return "Hello, World!"
And my microApp.py file:
from app import app
Here is my working directory:
My Project/
venv/
app/
__init__.py
routes.py
microApp.py
I set FLASK_APP = microApp.py and tried to run Flask. But the browser is throwing me an error.
Please anyone help me, I'm a noob. Thanks in advance.
When you are importing app that means your basically importing __init__.py file.
The __init__.py files are modules that initialize the packages.modules
So in your __init__.py file you importing again 'app'. Here you have to call 'routes'.
A server that does print "Hello world":
from flask import Flask
app = Flask(__name__)
#app.route("/")
#app.route('/index')
def hello_world():
return "Hello world!"
app.run('127.0.0.1', port=5500)

Flask TemplateNotFound

I am new to flask and try to run a very simple python file which calls an HTML document, but whenever I search on http://127.0.0.1:5000/, it raises the TemplateNotFound error. I searched stack overflow on similar questions, but even with implementing the solutions, I get the same error. Nothing worked so far
base.html contains:
<body>
<h1>Hello there</h1>
</body>
</html>
flask_test_2.py contains:
from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run()
as advised in some of the solutions, I checked the file structure:
/flask_test_2.py
/templates
/base.html
It should work. Since is doesn't, you may take out the
template_folder='templates' portion of you app` assignment and have it this way:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run()
it will use the templates directory by default.
The issue you are experiencing may be path related. You may also declare the app variable this way:
app = Flask(__name__, template_folder='usr\...\templates')

ModuleNotFoundError: Flask

I am calling a module inside urbancloth package from home.py
This runs the __init__ file but doesn't run another module present in urbancloth.
The directory structure is as follows -
The code in home.py:
from urbancloth import app
if __name__ == '__main__':
app.run(debug =True)
The code in __init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'b0fe7021532f0541f87226aafd71ec77'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
from urbancloth import routes
and the error message is-
I am following Sir CoreySchafers Flask tutorials
Github for original code
If you will look at you error closely
from urabncloth import app
Looking at your directory there is no urabncloth it is urbancloth. Just a typo. So in your routes.py change the package name.
Hope this solves your problem!!!

Why does my view function 404?

Directory Structure:
__init__:
from flask import flask
app = Flask(__name__)
if __name__ == '__main__'
app.run()
Views:
from app import app
#app.route('/')
def hello_world():
return 'Hello World!'
I hope someone can explain what I am doing wrong here -
I guess I'm not understanding how to properly import app. This results in a 404. However when views is moved back to __init__ everything works properly.
You need to explicitly import your views module in your __init__:
from flask import flask
app = Flask(__name__)
from . import views
Without importing the module, the view registrations are never made.
Do keep the script portion outside of your package. Add a separate file in Final_app (so outside the app directory) that runs your development server; say run.py:
def main():
from app import app
app.run()
if __name__ == '__main__'
main()

Categories