How to Fix ImportError: cannot import name ‘Callable’ from ‘traitlets’

Diagram of cannot import name Callable from traitlets

Diagram

ImportError: cannot import name ‘Callable’ from ‘traitlets’ error occurs because the “Callable class was removed from the traitlets module in version 5.1.0.” If you are using an older version of the traitlets module, you must upgrade to a newer version to use the Callable class.

How to fix it?

To fix the ImportError: cannot import name ‘Callable’ from ‘traitlets’ error, you can upgrade to a newer version of the “traitlets” module.

pip install --upgrade traitlets

Alternate solution

If you are unable to upgrade to a newer version of the traitlets module, you can use a different class to represent a callable object. For example, you could use the functools.partial class.

The functools.partial class is “used to create a partial function from a function and a set of arguments.”

For example, the following code will create a partial function from the function abs() and the argument 10

from functools import partial

abs_21 = partial(abs, 21)

print(abs_21)

Output

functools.partial(<built-in function abs>, 21)

That’s it!

Related posts

ModuleNotFoundError: No module named ‘traitlets’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.