我在Ubuntu上运行了同样的代码,没有问题,但在Windows10上运行它有问题。我还安装了Flask。我的windows环境配置如下:
$ pip --version
pip 21.1.1 from c:\users\min\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)
$ pip show flask
Name: Flask
Version: 2.0.0
Summary: A simple framework for building complex web applications.
Home-page: https://palletsprojects.com/p/flask
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\users\min\appdata\local\programs\python\python37\lib\site-packages
Requires: Werkzeug, itsdangerous, Jinja2, click
Required-by: Flask-RESTful, Flask-API
$ pip show flask-restful
Name: Flask-RESTful
Version: 0.3.8
Summary: Simple framework for creating REST APIs
Home-page: https://www.github.com/flask-restful/flask-restful/
Author: Twilio API Team
Author-email: help@twilio.com
License: BSD
Location: c:\users\min\appdata\local\programs\python\python37\lib\site-packages
Requires: Flask, aniso8601, six, pytz
Required-by:当我运行代码时,
from flask_restful import reqparse, Api, Resource错误是
Exception has occurred: ImportError
cannot import name '_endpoint_from_view_func' from 'flask.helpers' (C:\Users\Min\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\helpers.py)
File "E:\yulin\python_project\image_text_project_-api\chuanxian_api_module_time_native2.py", line 24, in <module>
from flask_restful import reqparse, Api, Resource我不知道为什么,请帮帮我,非常感谢。
发布于 2021-05-14 01:35:58
这是一个等待here解决的已知问题。
同时,我建议使用猴子补丁:
import flask.scaffold
flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
import flask_restful
...作为参考,我的Flask-RESTful包的当前版本是0.3.8。
编辑
由于该问题已在release 0.3.9中修复,因此只需升级程序包版本:
Flask-RESTful>0.3.8发布于 2021-05-26 09:51:59
就像雷纳托提到的,这是一个已知的问题。团队已经解决了这个问题。使用最新版本的Flask-RESTful解决了这个问题。例如:
Flask-RESTful==0.3.9发布于 2021-05-22 22:29:16
这是一个猴子补丁。您可以定义自己的_endpoint_from_view_func或使用@RenatoDamas的答案中提到的flask.scaffold._endpoint_from_view_func
# monkey.py
def _endpoint_from_view_func(view_func):
"""Internal helper that returns the default endpoint for a given
function. This always is the function name.
"""
assert view_func is not None, "expected view func if endpoint is not provided."
return view_func.__name__
# noinspection SpellCheckingInspection
def patch_restx(endpoint_from_view_func: bool = True) -> None:
"""
Nasty hacks are here
:param endpoint_from_view_func: Add missing function into flask for backward compatibility with `flask-restx`
"""
if endpoint_from_view_func:
func_name = "_endpoint_from_view_func"
try:
import pkg_resources
packages = pkg_resources.working_set.by_key
if "flask" in packages and "flask-restx" in packages:
flask_version = tuple(map(int, packages["flask"].version.split(".")))
flask_restx_version = tuple(map(int, packages["flask-restx"].version.split(".")))
if (2, 0, 0) <= flask_version and (1, 0, 0) > flask_restx_version:
import flask
if not hasattr(flask.helpers, func_name):
setattr(flask.helpers, func_name, _endpoint_from_view_func)
except ImportError:
print(f"skipping monkey patch of {func_name}")https://stackoverflow.com/questions/67496857
复制相似问题