Last updated: Apr 10, 2024
Reading timeยท8 min

The "ImportError: cannot import name 'BaseResponse' from 'werkzeug.wrappers'"
occurs because BaseResponse and BaseRequest have been deprecated starting
werkzeug version 2.1.0.
To solve the error, pin your werkzeug version to 2.0.3.

ImportError: cannot import name 'BaseResponse' from 'werkzeug.wrappers' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/wrappers/__init__.py) ImportError: cannot import name 'BaseRequest' from 'werkzeug.wrappers' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/wrappers/__init__.py)
werkzeug module to version 2.0.03Open your terminal and pin the werkzeug package to version 2.0.3, which is
the last version that exports BaseResponse and BaseRequest.
pip install werkzeug==2.0.3 pip3 install werkzeug==2.0.3 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install werkzeug==2.0.3 python3 -m pip install werkzeug==2.0.3 # ๐๏ธ py alias (Windows) py -m pip install werkzeug==2.0.3 # ๐๏ธ For Jupyter Notebook !pip install werkzeug==2.0.3

If you also use the jinja2 package, you'll likely have to pin it to version
3.0.3 as well.
pip install jinja2==3.0.3 pip3 install jinja2==3.0.3 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install jinja2==3.0.3 python3 -m pip install jinja2==3.0.3 # ๐๏ธ py alias (Windows) py -m pip install jinja2==3.0.3 # ๐๏ธ For Jupyter Notebook !pip install jinja2==3.0.3

You can also add the following line to your requirements.txt file.
werkzeug==2.0.3 jinja2==3.0.3

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
If you got the error when using the flask-lambda package, try upgrading its version.
pip install flask-lambda --upgrade pip3 install flask-lambda --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install flask-lambda --upgrade python3 -m pip install flask-lambda --upgrade # ๐๏ธ py alias (Windows) py -m pip install flask-lambda --upgrade # ๐๏ธ For Jupyter Notebook !pip install flask-lambda --upgrade

If you use werkzeug in your own codebase, make sure to use the correct import
statement depending on your version.
You can check your werkzeug version with the pip show command.
pip show werkzeug pip3 show werkzeug

The following import statement can be used for werkzeug < 2.1.0.
# โ werkzeug version < 2.1.0 from werkzeug.wrappers import BaseResponse, BaseRequest print(BaseResponse) print(BaseRequest)
And the following import statement can be used for werkzeug > 2.0.3.
# โ werkzeug version > 2.0.3 from werkzeug.wrappers import Response, Request print(Response) print(Request)

Alternatively, you can use a try/except statement to support both versions.
try: # ๐๏ธ Runs if werkzeug < 2.1.0 from werkzeug.wrappers import BaseResponse except ImportError: # ๐๏ธ Runs if werkzeug > 2.0.3 from werkzeug.wrappers import Response as BaseResponse print(BaseResponse)
The try statement tries to import BaseResponse from werkzeug.wrappers
(version < 2.1.0).
If the attempt fails, the except block runs and imports Response aliasing it
to BaseResponse (version > 2.0.3).
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py and run the file with python main.py to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following
command.
pip freeze > requirements.txt
The "ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'"
occurs when other modules try to access safe_str_cmp which has been removed
starting werkzeug v 2.1.0.
To solve the error, pin your werkzeug version to 2.0.3.

ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/security.py)
werkzeug version to 2.0.3Open your terminal and pin the werkzeug package to version 2.0.3, which is
the last version that exports safe_str_cmp.
pip install werkzeug==2.0.3 pip3 install werkzeug==2.0.3 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install werkzeug==2.0.3 python3 -m pip install werkzeug==2.0.3 # ๐๏ธ py alias (Windows) py -m pip install werkzeug==2.0.3 # ๐๏ธ For Jupyter Notebook !pip install werkzeug==2.0.3
If you also use the jinja2 package, you'll likely have to pin it to version
3.0.3 as well.
pip install jinja2==3.0.3 pip3 install jinja2==3.0.3 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install jinja2==3.0.3 python3 -m pip install jinja2==3.0.3 # ๐๏ธ py alias (Windows) py -m pip install jinja2==3.0.3 # ๐๏ธ For Jupyter Notebook !pip install jinja2==3.0.3
You can also add the following line to your requirements.txt file.
werkzeug==2.0.3 jinja2==3.0.3
When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
The
release notes of werkzeug 2.1.0
note that safe_str_cmp has been removed and the equivalent in the hmac
module should be used.
Flask if you use the packageIf the error persists and you use Flask, try upgrading your version of
Flask.
pip install flask --upgrade pip3 install flask --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install flask --upgrade python3 -m pip install flask --upgrade # ๐๏ธ py alias (Windows) py -m pip install flask --upgrade # ๐๏ธ For Jupyter Notebook !pip install flask --upgrade
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py and run the file with python main.py to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following
command.
pip freeze > requirements.txt
The "ImportError: cannot import name 'parse_rule' from 'werkzeug.routing'"
occurs because parse_rule is marked as internal in newer versions of
werkzeug, so it can't be used by other modules such as flask and
flask-login.
To solve the error, pin the werkzeug module to version 2.1.2.

ImportError: cannot import name 'parse_rule' from 'werkzeug.routing' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/routing/__init__.py)
Open your terminal and pin the werkzeug package to version 2.1.2.
pip install werkzeug==2.1.2 pip3 install werkzeug==2.1.2 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install werkzeug==2.1.2 python3 -m pip install werkzeug==2.1.2 # ๐๏ธ py alias (Windows) py -m pip install werkzeug==2.1.2 # ๐๏ธ For Jupyter Notebook !pip install werkzeug==2.1.2
You can also add the following line to your requirements.txt file.
werkzeug==2.1.2
When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
The parse_rule() function is marked as :internal: in recent versions of
werkzeug so it cannot be used by other packages.
If the error persists, try to pin Flask and werkzeug to version 2.1.2.
pip install werkzeug==2.1.2 pip3 install werkzeug==2.1.2 pip install flask==2.1.2 pip3 install flask==2.1.2
Make sure to update your requirements.txt file.
werkzeug==2.1.2 flask==2.1.2
The error often occurs when using flask-login, flask-restx or other packages
that use the parse_rule function under the hood.
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py and run the file with python main.py to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following
command.
pip freeze > requirements.txt
The "ImportError: cannot import name 'get_current_traceback' from
'werkzeug.debug.tbtools'" occurs because older versions of the plotly package
use get_current_traceback from werkzeug.
To solve the error, upgrade your version of plotly or pin werkzeug to
version 2.0.3.

ImportError: cannot import name 'get_current_traceback' from 'werkzeug.debug.tbtools' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/debug/tbtools.py)
dashIf you use the dash package, the first thing you should try is to upgrade your
version of dash.
pip install dash --upgrade pip3 install dash --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install dash --upgrade python3 -m pip install dash --upgrade # ๐๏ธ py alias (Windows) py -m pip install dash --upgrade # ๐๏ธ For Anaconda conda install -c conda-forge dash conda update dash # ๐๏ธ For Jupyter Notebook !pip install jupyter-dash --upgrade
werkzeug version to 2.0.3werkzeug version to 2.0.3 which is the last version that exports get_current_traceback.pip install werkzeug==2.0.3 pip3 install werkzeug==2.0.3 # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install werkzeug==2.0.3 python3 -m pip install werkzeug==2.0.3 # ๐๏ธ py alias (Windows) py -m pip install werkzeug==2.0.3 # ๐๏ธ For Jupyter Notebook !pip install werkzeug==2.0.3
You can also add the following line to your requirements.txt file.
werkzeug==2.0.3
When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
The following is a valid import when using werkzeug version 2.0.3.
from werkzeug.debug.tbtools import get_current_traceback
If you use a version greater than 2.0.3, you have to use the following import.
from werkzeug.debug.tbtools import DebugTraceback
If none of the suggestions helped and you're stuck using a version greater than
2.0.3, try adding the following 3 lines at the top of your entry point file.
import werkzeug from werkzeug.debug.tbtools import DebugTraceback werkzeug.debug.tbtools.get_current_traceback = DebugTraceback
The code sample imports DebugTraceback and sets the get_current_traceback
attribute to it.
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py and run the file with python main.py to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following
command.
pip freeze > requirements.txt