系统环境:
操作系统:openEuler20.03 SP3
python版本:系统自带3.7.9
GCC编译器:系统自带7.3.0
安装步骤:
yum install python3 python3-pip python3-devel
pip3 install virtualenv
python3 -m venv /usr/local/flask-restx-env
source /usr/local/flask-restx-env/bin/activate
pip3 install flask-restx==0.3.0 --trusted-host pypi.tuna.tsinghua.edu.cn -i https://pypi.tuna.tsinghua.edu.cn/simple

验证过程:
新建测试算例test.py
执行验证:python3 test.py
出现错误:

经分析:在flask2.0版本之后,flask.helpers模块中已经找不到_endpoint_from_view_func方法了,flask2.0版本后中将_endpoint_from_view_func方法修改到了flask.scaffold模块中。
修改/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/api.py,在from flask.helpers import _endpoint_from_view_func行前面加上两行:
import flask.scaffold
flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func

再次验证:出现错误cannot import name 'BaseResponse' from 'werkzeug.wrappers'

经分析,当前的werkzeug版本为2.2.3,BaseResponse是werkzeug1.0.1版本类封装,但由于werkzeug1.0.1版本会与flask2.0版本之后冲突,这里不能采用降级werkzeug的版本为1.0.1,werkzeug2.2.3版本的类封装叫Response
修改/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/api.py和/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/resource.py
分别将from werkzeug.wrappers import BaseResponse修改成from werkzeug.wrappers import Response as BaseResponse
api.py:

resource.py

再次验证:出现错误cannot import name 'parse_rule' from 'werkzeug.routing'

经分析:这是由于 parse_rule在 werkzeug 2.2.3版本上被标记为 :internal,此时需要将werkzeug降级到2.1.2版本,同时也需要将flask降级到2.1.2版本
pip3 install werkzeug==2.1.2 flask==2.1.2 --trusted-host pypi.tuna.tsinghua.edu.cn -i https://pypi.tuna.tsinghua.edu.cn/simple

再次验证:此时已启动成功, 在浏览器打开该地址:http://10.0.2.2:5000即可访问(注意:该IP不是固定的,根据用户的实际环境决定)


系统环境:
操作系统:openEuler20.03 SP3
python版本:系统自带3.7.9
GCC编译器:系统自带7.3.0
安装步骤:
yum install python3 python3-pip python3-devel
pip3 install virtualenv
python3 -m venv /usr/local/flask-restx-env
source /usr/local/flask-restx-env/bin/activate
pip3 install flask-restx==0.3.0 --trusted-host pypi.tuna.tsinghua.edu.cn -i https://pypi.tuna.tsinghua.edu.cn/simple
验证过程:
新建测试算例test.py
from flask import Flask from flask_restx import Api, Resource, fields app = Flask(__name__) api = Api(app, version='1.0', title='TodoMVC API', description='A simple TodoMVC API', ) ns = api.namespace('todos', description='TODO operations') todo = api.model('Todo', { 'id': fields.Integer(readonly=True, description='The task unique identifier'), 'task': fields.String(required=True, description='The task details') }) class TodoDAO(object): def __init__(self): self.counter = 0 self.todos = [] def get(self, id): for todo in self.todos: if todo['id'] == id: return todo api.abort(404, "Todo {} doesn't exist".format(id)) def create(self, data): todo = data todo['id'] = self.counter = self.counter + 1 self.todos.append(todo) return todo def update(self, id, data): todo = self.get(id) todo.update(data) return todo def delete(self, id): todo = self.get(id) self.todos.remove(todo) DAO = TodoDAO() DAO.create({'task': 'Build an API'}) DAO.create({'task': '?????'}) DAO.create({'task': 'profit!'}) @ns.route('/') class TodoList(Resource): '''Shows a list of all todos, and lets you POST to add new tasks''' @ns.doc('list_todos') @ns.marshal_list_with(todo) def get(self): '''List all tasks''' return DAO.todos @ns.doc('create_todo') @ns.expect(todo) @ns.marshal_with(todo, code=201) def post(self): '''Create a new task''' return DAO.create(api.payload), 201 @ns.route('/<int:id>') @ns.response(404, 'Todo not found') @ns.param('id', 'The task identifier') class Todo(Resource): '''Show a single todo item and lets you delete them''' @ns.doc('get_todo') @ns.marshal_with(todo) def get(self, id): '''Fetch a given resource''' return DAO.get(id) @ns.doc('delete_todo') @ns.response(204, 'Todo deleted') def delete(self, id): '''Delete a task given its identifier''' DAO.delete(id) return '', 204 @ns.expect(todo) @ns.marshal_with(todo) def put(self, id): '''Update a task given its identifier''' return DAO.update(id, api.payload) if __name__ == '__main__': app.run(debug=True,host='0.0.0.0',port=5000)执行验证:python3 test.py
出现错误:
经分析:在flask2.0版本之后,flask.helpers模块中已经找不到_endpoint_from_view_func方法了,flask2.0版本后中将_endpoint_from_view_func方法修改到了flask.scaffold模块中。
修改/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/api.py,在from flask.helpers import _endpoint_from_view_func行前面加上两行:
import flask.scaffold
flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
再次验证:出现错误cannot import name 'BaseResponse' from 'werkzeug.wrappers'
经分析,当前的werkzeug版本为2.2.3,BaseResponse是werkzeug1.0.1版本类封装,但由于werkzeug1.0.1版本会与flask2.0版本之后冲突,这里不能采用降级werkzeug的版本为1.0.1,werkzeug2.2.3版本的类封装叫Response
修改/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/api.py和/usr/local/flask-restx-env/lib64/python3.7/site-packages/flask_restx/resource.py
分别将from werkzeug.wrappers import BaseResponse修改成from werkzeug.wrappers import Response as BaseResponse
api.py:
resource.py
再次验证:出现错误cannot import name 'parse_rule' from 'werkzeug.routing'
经分析:这是由于 parse_rule在 werkzeug 2.2.3版本上被标记为 :internal,此时需要将werkzeug降级到2.1.2版本,同时也需要将flask降级到2.1.2版本
pip3 install werkzeug==2.1.2 flask==2.1.2 --trusted-host pypi.tuna.tsinghua.edu.cn -i https://pypi.tuna.tsinghua.edu.cn/simple
再次验证:此时已启动成功, 在浏览器打开该地址:http://10.0.2.2:5000即可访问(注意:该IP不是固定的,根据用户的实际环境决定)