ImportError: cannot import name 'parse_rule' from 'werkzeug.routing'

Question:

I got the following message after running my Flask project on another system.
The application ran all the time without problems:

Error: While importing 'app', an ImportError was raised:

Traceback (most recent call last):
  File "c:usersUserappdatalocalprogramspythonpython39libsite-packagesflaskcli.py", line 214, in locate_app
    __import__(module_name)
  File "C:UsersUserDesktopProjektapp__init__.py", line 3, in <module>
    from flask_restx import Namespace, Api
  File "c:usersUserappdatalocalprogramspythonpython39libsite-packagesflask_restx__init__.py", line 5, in <module>
  File "c:usersUserappdatalocalprogramspythonpython39libsite-packagesflask_restxapi.py", line 50, in <module>
    from .swagger import Swagger
  File "c:usersUserappdatalocalprogramspythonpython39libsite-packagesflask_restxswagger.py", line 18, in <module>
    from werkzeug.routing import parse_rule
ImportError: cannot import name 'parse_rule' from 'werkzeug.routing' (c:usersUserappdatalocalprogramspythonpython39libsite-packageswerkzeugrouting__i
nit__.py)

My requirements.txt

Flask~=2.1.2
psycopg2-binary==2.9.3
Flask-SQLAlchemy==2.5.1
flask-restx==0.5.1
qrcode~=7.3.1
PyPDF2==2.6.0
reportlab~=3.6.10
WTForms~=3.0.1
flask-bootstrap==3.3.7.1
flask-wtf==1.0.1
Asked By: ChsharpNewbie

||

Answers:

The solution (workaround) was that I additionally included werkzeug in the version 2.1.2 in the requirements.txt

werkzeug==2.1.2

more information at https://issuemode.com/issues/apache/airflow/111093941#answer-108883148

After that I could start the Flask application.

Answered By: ChsharpNewbie

UPDATE: This has now been fixed, you should be using version 1.0.3 or greater for flask-restx.

ORIGINAL: This is caused by parse_rule() being marked as :internal: on the latest release of werkzeug which impacts flask-restx (plus many others such as flask-login).

https://github.com/python-restx/flask-restx/issues/460 is open for flask-restx, https://github.com/maxcountryman/flask-login/issues/686 for flask-login.

As you’ve mentioned, pinning to an older version is the workaround for now (i.e. werkzeug==2.1.2).

Answered By: safe

Copying here from the github issue for those who find this. If you are using with Flask and try to downgrade to Werkzeug 2.1.2 you will get an error because flask wants 2.2.x from Werkzeug. You will need to downgrade flask as well as follows:

Werkzeug <= 2.1.2   
flask == 2.1.2   
flask-restx >= 0.5.1  
Answered By: Tanner Phillips

This Issue is come from latest version of flask. Based on most suggestions Flask was downgraded to v2.1.2. The latest version of Flask requires Werkzeug >=2.2.0 but Flask v2.1.2 requires Werkzeug >=2.0. Therefore Werkzeug was downgraded to v2.1.2.
I executed following commands because I used pipenv for dependency management.

pipenv install Flask==2.1.2
pipenv install Werkzeug==2.1.2

if someone using pip3 just execute following:

pip3 install Flask==2.1.2
pip3 install Werkzeug==2.1.2

After downgrading my Pipfile looks as follows:

[packages]
flask = "==2.1.2"
werkzeug = "==2.1.2"

Hope it will solve you issues!

Answered By: Nirmal Sankalana
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.