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

avatar
Borislav Hadzhiev

4 min

banner

# Table of Contents

  1. ImportError: cannot import name '_unicodefun' from 'click'
  2. Cannot import name 'ParameterSource' from 'click.core'

If you got the error "Cannot import name 'ParameterSource' from 'click.core'", click on the second subheading.

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

The "ImportError: cannot import name '_unicodefun' from 'click'" is caused because the black module imports _unicodefun from the click package which was removed in click version 8.1.0.

To solve the error, upgrade your version of the black module.

importerror cannot import name unicodefun from click

shell
ImportError: cannot import name '_unicodefun' from 'click' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/click/__init__.py)

# Update your version of the black module

A fix was introduced in black version 22.3.0, so to solve the error run the following command to upgrade black.

shell
pip install black click --upgrade pip3 install black click --upgrade # ๐Ÿ‘‡๏ธ if you don't have pip in PATH environment variable python -m pip install black click --upgrade python3 -m pip install black click --upgrade # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install black click --upgrade # ๐Ÿ‘‡๏ธ for Jupyter Notebook !pip install black click --upgrade

upgrade version of black

The commands update the black module to the latest version where the issue is fixed.

# Alternatively, you can install an older version of black

Alternatively, you can install version 22.3.0 of black, and not the latest version.

shell
pip install black==22.3.0 pip3 install black==22.3.0 # ๐Ÿ‘‡๏ธ if you don't have pip in PATH environment variable python -m pip install black==22.3.0 python3 -m pip install black==22.3.0 # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install black==22.3.0 # ๐Ÿ‘‡๏ธ for Jupyter Notebook !pip install black==22.3.0

install older version of black

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt

The pip freeze command will update the contents of your requirements.txt file to reflect the latest changes.

# Using the black command in a pre-commit hook

If you run black via a pre-commit hook, make sure to update your version of the black module.

environment.yml
- repo: https://github.com/psf/black rev: 22.10.0 hooks: - id: black

If the suggestion didn't help, try clearing your cache by running the following commands.

shell
pre-commit clean pre-commit autoupdate

# Updating your black.yml file

You can also update your black.yml file, if you have one, to use a more recent version of the black module.

black.yml
black.yml - name: install black run: | pip install black==22.10.0 - name: run black run: | black . --check --line-length 80

# Pinning your version of the click module to 8.0.4

An alternative solution is to pin your click version to 8.0.4, which is the last version that exports _unicodefun.

shell
pip install click==8.0.4 pip3 install click==8.0.4 # ๐Ÿ‘‡๏ธ if you don't have pip in PATH environment variable python -m pip install click==8.0.4 python3 -m pip install click==8.0.4 # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install click==8.0.4 # ๐Ÿ‘‡๏ธ for Jupyter Notebook !pip install click==8.0.4

pin click module to 8 0 4

If none of the suggestions helped, try upgrading all packages in your environment.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐Ÿ‘‡๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt

# Cannot import name 'ParameterSource' from 'click.core'

The "ImportError: cannot import name 'ParameterSource' from 'click.core'" occurs when we have an outdated version of the click module.

To solve the error, upgrade your versions of the click and black modules.

shell
ImportError: cannot import name 'ParameterSource' from 'click.core' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/click/core.py)

# Having an outdated version of the black module

The error is most commonly caused due to having an outdated version of the black module that has the click module as a dependency.

The first thing you should try is to upgrade your version of the black module.

shell
pip install black --upgrade pip3 install black --upgrade # ๐Ÿ‘‡๏ธ if you don't have pip in PATH environment variable python -m pip install black --upgrade python3 -m pip install black --upgrade # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install black --upgrade # ๐Ÿ‘‡๏ธ for Jupyter Notebook !pip install black --upgrade

If the suggestion didn't help, try upgrading your click module as well.

shell
pip install click --upgrade pip3 install click --upgrade # ๐Ÿ‘‡๏ธ if you don't have pip in PATH environment variable python -m pip install click --upgrade python3 -m pip install click --upgrade # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install click --upgrade # ๐Ÿ‘‡๏ธ for Jupyter Notebook !pip install click --upgrade

# Try to reinstall the black and click modules

If the error persists, try to uninstall the black and click modules and then install them.

shell
pip uninstall black click -y pip3 uninstall black click -y pip install black click pip3 install black click

If none of the suggestions helped, try to upgrade all packages in your environment.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐Ÿ‘‡๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev