python
module_error
ai_generated
true
AttributeError: <module 'unittest' from '.../unittest/__init__.py'> has no attribute 'mock'
ID: python/unittest-mock-not-callable-import
80%Fix Rate
90%Confidence
0Evidence
2024-01-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
The test uses `import unittest` and then `unittest.mock.patch`, but `unittest.mock` is not automatically imported as an attribute. It must be imported explicitly.
generic中文
测试使用 `import unittest` 后调用 `unittest.mock.patch`,但 `unittest.mock` 不会自动作为属性导入,必须显式导入。
Workarounds
-
97% success
Import mock explicitly: import unittest from unittest import mock class TestX(unittest.TestCase): @mock.patch('mymodule.func') def test_it(self, m): ... -
97% success
Or import the submodule directly: from unittest.mock import patch, MagicMock
Dead Ends
Common approaches that don't work:
-
95% fail
unittest is part of the standard library; installing a PyPI package named 'unittest' either fails or installs an unrelated shim that shadows the stdlib.
-
90% fail
Modifying the stdlib is fragile and the change is overwritten on interpreter upgrade.