Cannot import name 'BaseResponse' from 'werkzeug.wrappers'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
8 min

banner

# Table of Contents

  1. Cannot import name 'BaseResponse' from 'werkzeug.wrappers'
  2. Cannot import name 'safe_str_cmp' from 'werkzeug.security'
  3. Cannot import name 'parse_rule' from 'werkzeug.routing'
  4. Cannot import name 'get_current_traceback' from 'werkzeug.debug.tbtools'

# Cannot import name 'BaseResponse' from 'werkzeug.wrappers'

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

shell
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)

# Pin the werkzeug module to version 2.0.03

Open your terminal and pin the werkzeug package to version 2.0.3, which is the last version that exports BaseResponse and BaseRequest.

shell
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

pin werkzeug to version 2 0 3

If you also use the jinja2 package, you'll likely have to pin it to version 3.0.3 as well.

shell
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

pin jinja2 version to 3 0 3

You can also add the following line to your requirements.txt file.

requirements.txt
werkzeug==2.0.3 jinja2==3.0.3

update versions in requirements txt

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.".

However, even though the error is shown, the package is installed successfully.

# Update Flask-lambda if you use the package

If you got the error when using the flask-lambda package, try upgrading its version.

shell
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

update flask lambda package

# Using the correct import statements

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.

shell
pip show werkzeug pip3 show werkzeug

check werkzeug version

The following import statement can be used for werkzeug < 2.1.0.

main.py
# โœ… 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.

main.py
# โœ… werkzeug version > 2.0.3 from werkzeug.wrappers import Response, Request print(Response) print(Request)

import from werkzeug correctly

Alternatively, you can use a try/except statement to support both versions.

main.py
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.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
pip freeze > requirements.txt

# Cannot import name 'safe_str_cmp' from 'werkzeug.security'

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

shell
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/werkzeug/security.py)

# Pin your werkzeug version to 2.0.3

Open your terminal and pin the werkzeug package to version 2.0.3, which is the last version that exports safe_str_cmp.

shell
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.

shell
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.

requirements.txt
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.".

However, even though the error is shown, the package is installed successfully.

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.

# Upgrade your version of Flask if you use the package

If the error persists and you use Flask, try upgrading your version of Flask.

shell
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.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
pip freeze > requirements.txt

# Cannot import name 'parse_rule' from 'werkzeug.routing'

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

shell
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.

shell
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.

requirements.txt
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.".

However, even though the error is shown, the package is installed successfully.

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.

shell
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.

shell
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.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
pip freeze > requirements.txt

# Cannot import name 'get_current_traceback' from 'werkzeug.debug.tbtools'

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

shell
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)

# Upgrade your version of dash

If you use the dash package, the first thing you should try is to upgrade your version of dash.

shell
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

# Alternatively, pin your werkzeug version to 2.0.3

If the suggestion didn't help, you can pin your werkzeug version to 2.0.3 which is the last version that exports get_current_traceback.
shell
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.

requirements.txt
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.".

However, even though the error is shown, the package is installed successfully.

# Using the correct import statements

The following is a valid import when using werkzeug version 2.0.3.

main.py
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.

main.py
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.

main.py
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.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
pip freeze > requirements.txt
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev