Resolving ImportError: Cannot Import Name ‘_unicodefun’ From ‘click’

If you use the black code formatter in Python, you may encounter an error that says ImportError: cannot import name ‘_unicodefun’ from ‘click’. This error means that Python cannot find the _unicodefun module in the click package, which is a dependency of black. This article will give you a walkthrough about this error and methods to resolve it.

What is the “importerror: cannot import name ‘_unicodefun’ from ‘click’” error?

The “importerror: cannot import name ‘_unicodefun’ from ‘click’” error is a type of ImportError that occurs when you try to import a module or a function from another module, but Python cannot find it. For example, if you try to run this code:

from click import _unicodefun

You will get this error message:

importerror: cannot import name '_unicodefun' from 'click'

This means that Python cannot find the _unicodefun module in the click package. The click package library provides command-line interface tools for Python applications. The black package is a code formatter automatically formats Python code according to the PEP 8 style guide. Black depends on the click to handle its command line arguments and options.

What causes the “importerror: cannot import name ‘_unicodefun’ from ‘click’” error?

The main cause of this error is that you are using an incompatible version of click with black. The _unicodefun module was a private module in click that provided some helper functions for handling Unicode strings. However, this module was deprecated and removed in click version 8.1.0, released on April 1, 2023. Therefore, if you are using click version 8.1.0 or higher, you cannot import _unicodefun from it.

However, some older versions of black still try to import _unicodefun from click, which causes the error. For example, black version 20.8b1, which was released on August 24, 2022, has this line of code in its init.py file:

 importing _unicodefun

This means you will get the error if you are using black version 20.8b1 with click version 8.1.0 or higher.

How can the “importerror: cannot import name ‘_unicodefun’ from ‘click’” error be resolved?

To resolve this error you can use one of the two methods mentioned below: 

Upgrading the Black

One way is to upgrade black to version 22.3.0 or higher. This version of black has fixed the compatibility issue with click and does not try to import _unicodefun anymore. To upgrade black to version 22.3.0, you can use one of the following commands:

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

Updating Pre-commit file

While using black if you are using it as a part of pre-commit hoos’s YAML then you might need to update the .pre-commit-config.yaml with reference to the more recent version of black.

Syntax:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.1.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files
-   repo: https://github.com/psf/black
    rev: 22.1.0
    hooks:
    -   id: black
        exclude: ^dist/

Downgrade the click

Another way is to downgrade click to version 8.0.2 or lower. This click version still has the _unicodefun module and is compatible with older versions of black. To downgrade, click to version 8.0.2; you can use one of the following commands:

pip install --upgrade click==8.0.2 
# or if you have pip3: 
pip3 install --upgrade click==8.0.2

However, this method is not recommended because it may cause other issues with other packages that depend on click and require newer features or bug fixes.

Installing compatible click

There are some cases where the black can not be updated for example in Python 2.7 you can not update black due to dropped Python 2.7. For those situations you can install click= =8.0.4.

To install a particular version of click you can use the following code.

Syntax:

pip install <package_name>==<version>

Removing cache

Another way to resolve the ImportError: Cannot Import Name ‘_unicodefun’ From ‘click’ is to remove the trouble making cache which might be there from changes that were made in codes previously. To remove those cache you can use the following code.

Syntax:

pre-commit clean
pre-commit autoupdate

Setting black code formatter using pyproject.toml

Another solution to resolve the ImportError: Cannot Import Name ‘_unicodefun’ From ‘click’ is to set the black code formatter using pyproject.toml.

Syntax:

[tool.poetry.dev-dependencies]
black = {version = "^22.1.0", allow-prereleases = true}

[tool.black]
target-version = ["py39"]
line-length = 100
color = true

FAQs

Here are some frequently asked questions about this error:

Why did this error happen suddenly without me changing anything?

This error may happen if you have updated your packages automatically or manually without checking the compatibility with black. For example, if you have used pip install –upgrade or pip install -r requirements.txt commands, you may have upgraded your click package to version 8.1.0 or higher without noticing it.

How to prevent this error from happening again in the future?

To prevent this error from happening again, you should always check the compatibility of your packages before updating them. You can use pip show or pip list commands to see the current versions of your packages and their dependencies. You can also use pip freeze or pipreqs tools to generate a requirements.txt file specifying your packages’ exact versions. You can then use the pip install -r requirements.txt command to install the same versions of your packages on different environments.

Conclusion

In this article, you have learned what the “importerror: cannot import name ‘_unicodefun’ from ‘click’” error means, what causes it, and how to resolve it. You have also learned some tips on preventing this error from happening again and learning more about the click and black packages.

Reference

  1. Click
  2. Black

To learn more about some common errors follow Python Clear’s errors section.

Leave a Comment