问题现场:
按照官方文档创建demo,判断能否创建一个简单服务,并从web中显示:hello world。
# -*- encoding:utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
简单到不能再简单的demo,报错如下,原因:装了两个版本的Werkzeug,Werkzeug是用于实现 WSGI ,应用和服务之间的标准 Python 接口。
/Users/wang/anaconda3/envs/pytorch/bin/python /Users/wang/x/bert/predict_server.py
Traceback (most recent call last):
File "/Users/wang/x/bert/predict_server.py", line 2, in <module>
from flask import Flask
File "/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/flask/__init__.py", line 16, in <module>
from werkzeug.exceptions import abort
File "/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/__init__.py", line 151, in <module>
__import__('werkzeug.exceptions')
File "/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/exceptions.py", line 71, in <module>
from werkzeug.wrappers import Response
File "/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/wrappers/__init__.py", line 26, in <module>
from .common_descriptors import CommonRequestDescriptorsMixin
File "/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/wrappers/common_descriptors.py", line 6, in <module>
from ..http import dump_age
ImportError: cannot import name 'dump_age'
Process finished with exit code 1
解决方案:
将Werkzeug全部卸载,注意要卸载两次,因为你有两个版本的Werkzeug,然后再重新安装。
pip uninstall Werkzeug
pip uninstall Werkzeug
pip install Werkzeug
我卸载时的log:
(pytorch) localhost:~ wang$ pip uninstall Werkzeug
Found existing installation: Werkzeug 1.0.1
Uninstalling Werkzeug-1.0.1:
Would remove:
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/Werkzeug-1.0.1.dist-info/*
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/*
Would not remove (might be manually added):
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/__init__.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/atom.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/cache.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/fixers.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/iterio.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/jsrouting.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/limiter.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/lint.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/profiler.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/securecookie.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/sessions.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/testtools.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/contrib/wrappers.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/script.py
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug/wrappers.py
Proceed (y/n)? y
Successfully uninstalled Werkzeug-1.0.1
(pytorch) localhost:~ wang$
(pytorch) localhost:~ wang$ pip uninstall Werkzeug
Found existing installation: Werkzeug 0.12.2
Uninstalling Werkzeug-0.12.2:
Would remove:
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/Werkzeug-0.12.2-py3.6.egg-info
/Users/wang/anaconda3/envs/pytorch/lib/python3.6/site-packages/werkzeug
Proceed (y/n)? y
Successfully uninstalled Werkzeug-0.12.2
(pytorch) localhost:~ wang$ pip uninstall Werkzeug
WARNING: Skipping Werkzeug as it is not installed.
(pytorch) localhost:~ wang$ pip install Werkzeug
Collecting Werkzeug
Using cached Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Installing collected packages: Werkzeug
Successfully installed Werkzeug-1.0.1
WARNING: You are using pip version 20.2.3; however, version 20.2.4 is available.
You should consider upgrading via the '/Users/wang/anaconda3/envs/pytorch/bin/python -m pip install --upgrade pip' command.
再次运行这个demo可以了:
/Users/wang/anaconda3/envs/pytorch/bin/python /Users/wang/x/bert/predict_server.py
* Serving Flask app "predict_server" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)/Users/wang/anaconda3/envs/pytorch/bin/python /Users/wang/x/bert/predict_server.py
* Serving Flask app "predict_server" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
点击链接可以 看到:hello world !如下:
1.官方文档:https://dormousehole.readthedocs.io/en/latest/quickstart.html#quickstart
2.https://www.jianshu.com/p/d537d634df7b
3.https://stackoverflow.com/questions/57753485/importerror-cannot-import-name-dump-age