So I'm following a beginners tutorial on Flask and for whatever reason am getting an error on what is essentially the very first step.
I first created an "app" directory where I created a python file for "init.py" which contains the following code:
from flask import Flask
app = Flask(__name__)
from app import routes
I then created a "routes.py" python file in the same directory:
from app import app
#app.route('/')
#app.route('/index')
def index():
return "Hello, World!"
Finally (and this is where the problem stems from), I created a python file named "microblog.py" which is located in the same folder as the "app" directory:
from app import app
I then go to my virtual environment and run (using cmd windows):
set FLASK_APP=microblog.py
So far so good, however when I try to run the following code in cmd:
flask run
I get the following error:
ImportError: cannot import name 'app' from 'app' (C:\Users\Grae_\microblog\app\__init__.py)
If any further clarification is needed, here are my file locations:
C:\Users\Grae_\microblog
C:\Users\Grae_\microblog\app
C:\Users\Grae_\microblog\__init__.py
C:\Users\Grae_\microblog\routes.py
C:\Users\Grae_\microblog\venv
C:\Users\Grae_\microblog\microblog.py
Apologies if this is really obvious, I'm just obviously very new to Flask and have been stuck on this for a while.
Thanks
The issue here is on python package "app". The directory should have a file named __init__.py instead of init.py.
For example, you rename the file init.py to __init__.py and replace content with below code it should work
from flask import Flask
app = Flask(__name__)
def start():
from app import routes
start()
You can do something like this:-
test.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Index!"
#app.route("/hello")
def hello():
return "Hello World!"
#app.route("/members")
def members():
return "Members"
#app.route("/members/<string:name>/")
def getMember(name):
return name</string:name>
if __name__ == "__main__":
app.run()
In command prompt, run the command-
python test.py
Try the URLs in your browser:
http://127.0.0.1:5000/
http://127.0.0.1:5000/hello
http://127.0.0.1:5000/members
http://127.0.0.1:5000/members/Karan/
Related
Sorry if this is a bit basic but wanted to validate what my flask_app name would be that I set in my .env file when running locally.
I run my app using a wsgi.py file in root with the following contents:
from app import create_app, db
application = create_app()
if __name__ == '__main__':
application.run()
However I then have a app/init.py
import os
from config import Config
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
#not sure if i mucked this up
db.init_app(app)
from app.main import main_blueprint
app.register_blueprint(main_blueprint)
return app
Which I run with flask run. Therefore is my app name “app” or “application” or even something else?
You want flask_app variable to be the name of the file which runs the app, so for you in your .env file it looks like you should have:
FLASK_APP = run.py
where run.py is the name of the file containing the first block of code in your question, assuming that your .env file is also in your root folder, let me know if that helps.
I'm using Python 3.7.8
ModuleNotFound: No module named 'SpeechRecognition' appears when I try to run my project.
ModuleNotFound: No module named 'SpeechRecognition'
But it works well when I try it with python -m speech_recognition:
enter image description here
How to start flask app. I recommend to start like this. main.py is flask start script file.
# set flask start script file
export FLASK_APP=main.py
# run flask
python3 -m flask run
# main.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
My Sample Flask Code (wsgitest.py):
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route('/wait')
def index():
return ("<h3>Hello World</h3>")
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=5000)
waitress
Version: 1.4.3
Error
ImportError: cannot import name 'serve' from 'waitress' (C:\.....\wsgitest.py)
if your your project has one file named waitress. Import resolves to your project file rather than waitress package
your code is ok. I copy your code to my idea, it's run successfully
Hi I don't know why "app" is not defined. I have a very simple app and try to run it:
run.py:
from flask import Flask
app = Flask(__name__)
import views
if __name__ == "__main__":
app.run(debug=True)
views.py:
#app.route('/')
def hello():
return 'Hello, World!'
If I try to run the server via "python run.py" I get the following error:
File "XXX\a\views.py", line 1, in <module>
#app.route('/')
NameError: name 'app' is not defined
You need to put all your routes in run.py.
This is because, you have declared app in run.py and in views.py you are trying to access it.
Following works for me:
run.py
from flask import Flask
app = Flask(__name__)
import view
view.py
from run import app
#app.route('/')
def hello():
return 'Hello, World!'
On terminal execute
export FLASK_ENV=development
export FLASK_APP=run.py
then finally flask run or python -m flask run.
Also, its a good idea to switch to flask run from app.run. See this
But when I run the file it shows that flask could not import my python file.
I am having trouble to understand why flask shows this error.
THis is the image of my command prompt
https://imgur.com/TQaI6m3
This is the link of image of my command prompt. I am using Anaconda3
//This is the code in my flask_test.py file.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
Try this code
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()