ImportError: cannot import name 'apply' from 'dask.compatibility'
Third-person routing text. Fixes ImportError for apply from dask.compatibility: dask removed the Python 2 compatibility shim. Replace the import with the builtin apply-equivalent or upgrade the dependent library. Use when an agent hits this on old code with new dask. Not for other dask.compatibility names.
TL;DR: dask.compatibility.apply was a Python 2 shim that dask removed. Stop importing it (just call the function directly on Python 3) or upgrade the library that does the old import.
The error
ImportError: cannot import name 'apply' from 'dask.compatibility'Fix it
- In your code, drop the shim. The old pattern:
from dask.compatibility import apply
apply(func, args)becomes:
func(*args)Expected: identical behavior on Python 3.
- If the bad import is inside a third-party package, upgrade that package to a release that supports modern dask:
pip install -U [the-package]- Verify the import that failed now works.
When this applies
Use this when code does from dask.compatibility import apply on a recent dask.
When it does not apply
If the error names a different dask.compatibility member, check whether dask still ships it. If dask itself will not import, reinstall dask.
Compatibility
Recent dask versions (the shim was removed after the Python 2 drop). The direct-call replacement works on all Python 3.
Root cause
dask.compatibility held shims for running on both Python 2 and 3. Once dask dropped Python 2, the shims were deleted, and anything still importing them breaks at import time.
Edge cases
Other removed shims in the same module include unicode and long-related helpers; grep your codebase for dask.compatibility imports when upgrading dask. Libraries should not import from dask.compatibility at all anymore.