• Home
  • Jobs
  • Courses
  • Teachers
  • For business
  • Blog
  • ES/EN

0

31
Views
cannot import name '_endpoint_from_view_func' from 'flask.helpers' in python

I ran the same code on Ubuntu with no problem, but ran it on Windows10 with problems. I also installed Flask. My windows environment is configured as follows:

$ 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:

When I run the code, there is wrong in

from flask_restful import reqparse, Api, Resource

and error is

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

I don't know why, please help me, thank you very much.

about 1 month ago ·

Santiago Trujillo

3 answers
Answer question

0

That's a known issue that awaits to be solved here.

In the meantime, I suggest a monkey patching:

import flask.scaffold
flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
import flask_restful
...

For reference, the current version of my Flask-RESTful package is 0.3.8.


Edit:

Since the issue has then been fixed in release 0.3.9, just upgrade the package version:

Flask-RESTful>0.3.8
about 1 month ago · Santiago Trujillo Report

0

Like Renato mentioned, this is a known issue. The team has fixed it. Using a recent version of Flask-RESTful fixed the issue for me. For example:

Flask-RESTful==0.3.9
about 1 month ago · Santiago Trujillo Report

0

Here is a monkey patch. You can define your own _endpoint_from_view_func or use flask.scaffold._endpoint_from_view_func as mentioned in answer of @RenatoDamas

# 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}")
about 1 month ago · Santiago Trujillo Report
Answer question
Remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Startups
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.