How to solve cannot import name 'abort' from 'werkzeug.exceptions' error while importing Flask
Question:
I am getting error while importing flask,
error message goes as
ImportError: cannot import name ‘abort’ from ‘werkzeug.exceptions’
here is my full code
from flask import Flask,render_template,request,jsonify
from sklearn.preprocessing import StandardScaler
import pickle
app=Flask(__name__)
@app.route('/',methods=['GET','POST'])
def homepage():
print("home")
return render_template("Index.html")
@app.route('/predict',methods=['POST'])
def predict():
print("predict")
preg=int(request.form["PreganciesName"])
glucose=float(request.form["GlucoseName"])
BP=float(request.form["BloodPressureName"])
st=int(request.form["SkinThicknessName"])
insu=float(request.form["InsulinName"])
bmi=float(request.form["BMIName"])
dpf=float(request.form["DPFName"])
age=int(request.form["ageName"])
print("fetching data done")
scaler=StandardScaler()
filename = 'Diabetis with logistic regression.pickle'
loaded_model = pickle.load(open(filename, 'rb'))
print("model loded")
a = loaded_model.predict(scaler.transform([[preg,glucose,BP,st,insu,bmi,dpf,age]]))
print("otput "+str(a))
return render_template('result.html',a[0])
if __name__=="__main__":
app.run(debug=True)
Answers:
There is no import of abort
in the code that you posted.
However, have a look where the error is raised and replace the abort
that you’re wrongly importing with:
from flask import abort
I guess problem was with python 3.8 version. I reinstalled python 3.6 and it worked. I think it will also work fine for python 3.7
On Linux, I had the same error message for the flask package for python2.7:
File "/usr/local/lib/python2.7/dist-packages/flask/init.py", line
17, in from werkzeug.exceptions import abort ImportError: No
module named werkzeug.exceptions
I could reproduce this error by running
from flask import Flask
from flask import abort
from werkzeug.exceptions import abort
and the following command fixed it:
sudo apt-get install python-flask
For Python 3, it should be:
sudo apt-get install python3-flask
No need to guess, look it up with:
$ apt-cache search "python.*flask$"
python3-flask - micro web framework based on Werkzeug and Jinja2 - Python 3.x
python3-frozen-flask - Freezes a Flask application into a set of static files
python3-pylint-flask - Pylint plugin for analyzing Flask applications (Python 3)
python3-pytest-flask - pytest plugin to test Python Flask - Python 3 version
python3-typeshed - collection of library stubs for Python, with static types
This solution can be generalised, see Python error "ImportError: No module named".
I am getting error while importing flask,
error message goes as
ImportError: cannot import name ‘abort’ from ‘werkzeug.exceptions’
here is my full code
from flask import Flask,render_template,request,jsonify
from sklearn.preprocessing import StandardScaler
import pickle
app=Flask(__name__)
@app.route('/',methods=['GET','POST'])
def homepage():
print("home")
return render_template("Index.html")
@app.route('/predict',methods=['POST'])
def predict():
print("predict")
preg=int(request.form["PreganciesName"])
glucose=float(request.form["GlucoseName"])
BP=float(request.form["BloodPressureName"])
st=int(request.form["SkinThicknessName"])
insu=float(request.form["InsulinName"])
bmi=float(request.form["BMIName"])
dpf=float(request.form["DPFName"])
age=int(request.form["ageName"])
print("fetching data done")
scaler=StandardScaler()
filename = 'Diabetis with logistic regression.pickle'
loaded_model = pickle.load(open(filename, 'rb'))
print("model loded")
a = loaded_model.predict(scaler.transform([[preg,glucose,BP,st,insu,bmi,dpf,age]]))
print("otput "+str(a))
return render_template('result.html',a[0])
if __name__=="__main__":
app.run(debug=True)
There is no import of abort
in the code that you posted.
However, have a look where the error is raised and replace the abort
that you’re wrongly importing with:
from flask import abort
I guess problem was with python 3.8 version. I reinstalled python 3.6 and it worked. I think it will also work fine for python 3.7
On Linux, I had the same error message for the flask package for python2.7:
File "/usr/local/lib/python2.7/dist-packages/flask/init.py", line
17, in from werkzeug.exceptions import abort ImportError: No
module named werkzeug.exceptions
I could reproduce this error by running
from flask import Flask
from flask import abort
from werkzeug.exceptions import abort
and the following command fixed it:
sudo apt-get install python-flask
For Python 3, it should be:
sudo apt-get install python3-flask
No need to guess, look it up with:
$ apt-cache search "python.*flask$"
python3-flask - micro web framework based on Werkzeug and Jinja2 - Python 3.x
python3-frozen-flask - Freezes a Flask application into a set of static files
python3-pylint-flask - Pylint plugin for analyzing Flask applications (Python 3)
python3-pytest-flask - pytest plugin to test Python Flask - Python 3 version
python3-typeshed - collection of library stubs for Python, with static types
This solution can be generalised, see Python error "ImportError: No module named".