Recently when running my lint checks with the Python Black package, an unexpected error came up. Here is a snippet of the error I got.
ImportError: cannot import name '_unicodefun' from 'click' (/Users/robot/.cache/pre-commit/repo3u71ccm2/py_env-python3.9/lib/python3.9/site-packages/click/init.py)`
Here are the steps I used to solve the error ImportError: cannot import name ‘_unicodefun’ from ‘click.’
Why ImportError: cannot import name ‘_unicodefun’ from ‘click’ error is seen?
The error, ImportError: cannot import name ‘_unicodefun’ from ‘click’ is seen because an older version black in python. Black 22.3.0 before shows an import error; this issue has been fixed in the latest version of black.
For a simple fix, just update the pip version and install the latest version of black
A detailed solution to fix the ImportError: cannot import name ‘_unicodefun’ from ‘click’ error provided below:
How to fix the ImportError: cannot import name ‘_unicodefun’ from ‘click’ error ?
The error ImportError: cannot import name ‘Markup’ from ‘jinja2’ can be solved just by upgrading the pip and installing the latest version of black.
Step 1: Upgrade to Black 22.3.0. Version
This error, ImportError: cannot import name ‘_unicodefun’ from ‘click’ ;
is fixed in the latest version of Black 22.3.0. Versions preceding 22.3.0 will not work with click 8.1.0.
A code snippet of black.yml:
python-version: 3.8
- name: install black
run: |
- pip install black==20.8b1
+ pip install black==22.3.0
- name: run black
run: |
black . --check --line-length 100
As an alternate way around this, pin click
to the last version via pip install --upgrade click==8.0.2
.
Step 2: Update the pre-commit File
In case you are planing to use black
as part of a pre-commit hook’s YAML, you will have to update the pre-commit file (often .pre-commit-config.yaml
) to reference the latest versions of black. Here is a snippet of how the code looks.
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.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.3.0
hooks:
- id: black
exclude: ^dist/
Just running the pip install of a latest version of black
will not be sufficient for command-line pre-commit hooks if the YAML file pins black
itself to a particular version… which pre-commit
does by default
If none of the above works, you might have troublemaking cache from previous code changes. Try running:
pre-commit clean
pre-commit autoupdate
Conclusion
To fix the error ImportError: cannot import name ‘_unicodefun’ from ‘click’; try updating the pip version and install the latest version of black.
If you’re using black
as part of a pre-commit hook’s YAML, you can update the pre-commit file (often .pre-commit-config.yaml
) to reference the more recent versions of black. If none of the above works, you might have trouble making cache from previous code changes. Try running some cleaning codes to fix the error.