Python is a versatile programming language that is well-accepted all over the world as it helps programmers design web applications and web pages. It has written in a simpler syntax that makes it an ideal one for even beginners. When working with Python, programmers and developers use Flask, which is a microweb framework. With this, you can create web applications simple that is implemented on Werkzeug. The Werkzeug is a library collection that is used to build Web Server Gateway Interface (WSGI) web applications in Python. When working with flask and Werkzeug, you may encounter the error “ImportError: cannot import name ‘parse_rule’ from ‘werkzeug.routing”.
Go ahead with the post to know how you can solve the error. Let’s start discussing how you get the error warning
How the error occurs
When you are working on a flask project on some other system, you end up with the error warning. This is what you get:
Traceback (most recent call last): File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\flask\cli.py", line 214, in locate_app __import__(module_name) File "C:\Users\User\Desktop\Projekt\app\__init__.py", line 3, in <module> from flask_restx import Namespace, Api File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\flask_restx\__init__.py", line 5, in <module> File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\flask_restx\api.py", line 50, in <module> from .swagger import Swagger File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\flask_restx\swagger.py", line 18, in <module> from werkzeug.routing import parse_rule ImportError: cannot import name 'parse_rule' from 'werkzeug.routing' (c:\users\User\appdata\local\programs\python\python39\lib\site-packages\werkzeug\routing\__i nit__.py)
It happens when you use the following script:
The requirements.txt includes:
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==184.108.40.206 flask-wtf==1.0.1
All in all, this is how you land up in the trouble. Let’s check out how to remove the error efficiently
How To Remove the Error “ImportError: cannot import name ‘parse_rule’ from ‘werkzeug.routing”
The prime cause of the error is the Werkzeug version is outdated. If you keep using the old version you may get a few or even some functions not supported. Even the most recent flask version causes the error. It is recommended to downgrade the flask to version v2.1.2.
However, the recent flask version needs you to have Werkzeug >=2.2.0. In case you have flask version v2.1.2 needs to have Werkzeug >=2.0. So, downgrading Werkzeug to V2.1.2. If you are using pipenv, you can use the following command for dependency management:
pipenv install Flask==2.1.2 pipenv install Werkzeug==2.1.2
If you are using pip3, execute the command below:
pip3 install Flask==2.1.2 pip3 install Werkzeug==2.1.2
Once you downgrade the Pipfile, it looks something like this:
[packages] flask = "==2.1.2" werkzeug = "==2.1.2"
It is an effective solution to help you resolve the issue.
In this post, we shed light on the solution to remove the error “ImportError: cannot import name ‘parse_rule’ from ‘werkzeug.routing”. This one is the effective way you can easily implement. I hope you find it interesting, informative, and helpful.
Don’t shy away if you need any further assistance. If you like the post, you can write your feedback in the below comment box. Your response is highly appreciated.