Python ImportError: cannot import name '_unicodefun' from 'click'

The click package may cause an error when you run the black code formatter.

This article will help you learn how to resolve the error.

Why ImportError: cannot import name ‘_unicodefun’ from ‘click’ occurs

When running Black code formatter in Python, you might encounter an error that says:

Traceback (most recent call last):
  File ...
    from click import _unicodefun
ImportError: cannot import name '_unicodefun' from 'click'

This ImportError occurs because the black package was trying to import _unicodefun module from the click package.

The _unicodefun module has been deprecated and removed in click version 8.1.0.

How to resolve ImportError: cannot import name ‘_unicodefun’ from ‘click’

To fix this error, you need to upgrade black to version 22.3.0 or newer.

To upgrade black to version 22.3.0, use one of the following commands:

pip install black==22.3.0

# or if you have pip3:
pip3 install black==22.3.0

if you run black using pre-commit hook, then you can update the rev tag in pre-commit-config.yaml as follows:

- repo: https://github.com/psf/black
  rev: 22.3.0
  hooks:
    - id: black

The error should be fixed the next time you run the pre-commit hook.

If you still see the error, then the best option is to upgrade both black and click to the latest versions.

Run one of the following commands from the terminal:

pip install --upgrade black click

# or if you have pip3:
pip3 install --upgrade black click

Once both packages have been upgraded to their latest versions, the error should now be resolved.

Conclusion

The error ImportError: cannot import name '_unicodefun' from 'click' occurs in Python when you run an older version of the black package.

This is because the click package removed _unicodefun in version 8.1.0.

To fix this error, you need to use black version 22.3.0 or newer, or upgrade both black and click to the latest versions.

Nice work solving this error. See you in other articles! 🍻

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.