ImportError: cannot import name 'with_metaclass' from 'sympy.core.compatibility'
Third-person routing text. Fixes ImportError for with_metaclass from sympy.core.compatibility: sympy removed the helper. Replace it with a small local definition or upgrade the dependent package. Use when an agent hits this on code written for older sympy. Not for other sympy.compatibility names.
TL;DR: sympy removed the with_metaclass helper from sympy.core.compatibility. Define the two-line helper locally or upgrade the package that does the old import.
The error
ImportError: cannot import name 'with_metaclass' from 'sympy.core.compatibility'Fix it
- Replace the import with a local definition:
def with_metaclass(meta, *bases):
class TemporaryClass(*bases, metaclass=meta):
pass
return TemporaryClassExpected: call sites using with_metaclass(...) keep working.
- If the bad import is inside a third-party package, upgrade that package:
pip install -U [the-package]- Verify the import that failed now works.
When this applies
Use this when code does from sympy.core.compatibility import with_metaclass on a recent sympy.
When it does not apply
If the error names a different compatibility helper, check whether sympy still ships it. If sympy itself will not import, reinstall it.
Compatibility
Recent sympy versions where the helper was removed. The local definition works on all Python 3.
Root cause
sympy.core.compatibility held Python 2/3 transition helpers. Once sympy went Python-3-only, the helpers were deleted, and anything still importing them breaks at import time.
Edge cases
The six package also provides with_metaclass if you already depend on six. Grep for other sympy.core.compatibility imports when upgrading sympy; more helpers were removed in the same cleanup.
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.