ImportError: cannot import name 'VisibleDeprecationWarning' from 'numpy'
Third-person routing text. Shows how to fix ImportError for VisibleDeprecationWarning from numpy: the name was removed in numpy 2.0, so the fix is to stop importing it from numpy and use Python's builtin warnings module instead, or pin numpy below 2.0. Use when an agent hits this import error on numpy 2.x. Not for other removed numpy names.
TL;DR: VisibleDeprecationWarning was removed in numpy 2.0, so from numpy import VisibleDeprecationWarning cannot work anymore. Replace it with Python's builtin warnings module. If you cannot change the code, pin numpy below 2.0 as a stopgap.
The error
ImportError: cannot import name 'VisibleDeprecationWarning' from 'numpy'Fix it
- Find every place the removed name is imported:
grep -rn "VisibleDeprecationWarning" [your project dir] Expected: a list of files doing from numpy import VisibleDeprecationWarning.
- Replace the import with the stdlib warnings module:
import warnings and where the code raised or filtered it, use warnings.warn(msg, DeprecationWarning) or the builtin DeprecationWarning class. Expected: no remaining references to the numpy import.
- Re-run the import that failed. Expected: it imports cleanly.
- If you cannot edit the code (a pinned third-party package), downgrade numpy instead:
pip install "numpy<2.0"Expected: the old import works again.
When this applies
Use this when the traceback points at from numpy import VisibleDeprecationWarning and numpy 2.x is installed.
When it does not apply
If the error names a different removed symbol (np.float, np.unicode_, np.asfarray), each needs its own replacement. If numpy is still 1.x, something else is wrong.
Compatibility
numpy 2.0 and later. VisibleDeprecationWarning existed through numpy 1.26.
Root cause
NumPy 2.0 did a long-planned cleanup of deprecated names. VisibleDeprecationWarning was a thin subclass of UserWarning, so the stdlib warnings module plus DeprecationWarning is the intended replacement.
Edge cases
Some libraries vendored the name; upgrading the library to a numpy-2-compatible release is better than patching. If tests filter on the warning class, update the filter to DeprecationWarning too.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.