pythonpython-requests

Python requests: No module named 'werkzeug.wrappers.json'


I'm trying to use the requests library in Python, but when I do so, I get an error

    from werkzeug.wrappers.json import JSONMixin
ModuleNotFoundError: No module named 'werkzeug.wrappers.json'

Below is an example (using a repo I forked specially for this, and a very fine grained access token that will expire soon, in case you're concerned about me sharing it).

import requests import json

# GitHub API URL to create an issue in the crobarcro/Spoon-Knife repository
url = 'https://api.github.com/repos/crobarcro/Spoon-Knife/issues'

# Example payload for creating a GitHub issue
payload = {
    "title": "Test Issue from API",
    "body": "This is a test issue created via the GitHub API in the crobarcro/Spoon-Knife repository.",
    "assignees": [],  # You can assign this to specific users if needed
    "labels": ["bug"]  # Optional, you can add labels to the issue
}

# Headers with authentication (you need a GitHub token)
headers = {
    'Authorization': 'token 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',  # Replace with your GitHub token
    'Accept': 'application/vnd.github.v3+json',
    'Content-Type': 'application/json'
}

# Sending the POST request
response = requests.post(url, headers=headers, data=json.dumps(payload))

# Checking the response
if response.status_code == 201:
    print('Issue created successfully:', response.json())
else:
    print(f'Failed to create issue. Status code {response.status_code}:', response.text)

The full error message:

Traceback (most recent call last):
  File "/snap/pycharm-community/405/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py", line 75, in <module>
    sys.exit(pytest.main(args, plugins_to_load + [Plugin]))
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 159, in main
    config = _prepareconfig(args, plugins)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 346, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/helpconfig.py", line 106, in pytest_cmdline_parse
    config = yield
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1152, in pytest_cmdline_parse
    self.parse(args)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1501, in parse
    self._preparse(args, addopts=addopts)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1388, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/home/<redacted>/.local/lib/python3.10/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
    exec(co, module.__dict__)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/schemathesis/__init__.py", line 2, in <module>
    from ._hypothesis import init_default_strategies, register_string_format
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
    exec(co, module.__dict__)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/schemathesis/_hypothesis.py", line 13, in <module>
    from . import utils
  File "/home/<redacted>/.local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
    exec(co, module.__dict__)
  File "/home/<redacted>/.local/lib/python3.10/site-packages/schemathesis/utils.py", line 17, in <module>
    from werkzeug.wrappers.json import JSONMixin
ModuleNotFoundError: No module named 'werkzeug.wrappers.json'

Solution

  • While I am not exactly sure why werkzeug is imported by the requests library, I know that the werkzeug.wrappers.json import has been deprecated from the werkzeug library. If you need it, you could downgrade werkzeug to an older version via:

    pip install "werkzeug<1"
    

    A better solution would probably to check your version of the requests library, and implement your script according to documentation for that version.