. Advertisement .
..3..
. Advertisement .
..4..
Python learners often encounter various errors when they try to work on a project. Among several errors, ModuleNotFoundError: No module named ‘click’ is one of those errors that make the beginners worried on how to get through it.
In this article, we are going to discuss this error in detail, and also provide the solution to make it simpler for you.
Let’s start with what exactly is it
What is ModuleNotFoundError: No module named ‘click’?
ModuleNotFoundError: No module named ‘click’ is an error that occurs when trying to import the click module without installing it or installing it incorrectly. The reason that leads to this error includes
- Use the module without installing it
- IDE is set to the wrong version of Python
- Working on a virtual environment without having to install the click module inside a virtual environment
- Install a different version of the click package than the python version
- Stating a variable name as the module name
How to Fix ModuleNotFoundError: No module named ‘click’?
Now that you know what is ModuleNotFoundError: No module named ‘click’ and its causes of, let’s checkout how to fix it
Install Click Module
According to the python version and operating system you are using, install click module using click module
# If you are using Python 2 (Windows)
pip install click
# if you are using Python 3 (Windows)
pip3 install click
# If the pip is not set as environment varibale PATH
python -m pip install click
# If you are using Python 2 (Linux)
sudo pip install click
# if you are using Python 3 (Linux)
sudo pip3 install click
# In case if you have to easy_install
sudo easy_install -U click
# On Centos
yum install click
# On Ubuntu
sudo apt-get install click
# If you are installing it in Anaconda
conda install -c conda-forge click
After installing the click module, just import it into your code like this
import click
@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name", help="The person to greet.")
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for _ in range(count):
click.echo(f"Hi, {name}!")
if __name__ == '__main__':
hello()
Output
$ python hello.py --count=3
Your name: Click
Hi, Click!
Hi, Click!
Hi, Click!
Check the ID set to the Correct Version
If the error persists after installing the package, check if the IDE is set to the correct version of Python you are using.
Open the command palette by pressing CTRL + Shift + P
or (⌘ + Shift + P
on Mac). Once it opens, select the correct Python Version.
main.py - Python_Samples - Visual Studio Code
Select Interpreter
Selected Interpreter: .venv\Scripts\python.exe
+ Enter interpreter path...
Python 3.10.0 ('venv': venv) . veny\Scripts\python.exe Recommended
Python 3.10.0 Programs\i-dA08bub8-py3.10') - \AppData\Local\pypoetryCache\virtualenvsppc... Poetry Python 3.10.0 64-bit -\AppData\Local\Programs\Python\Python310\python.exe Global
Python 3.7.0 64-bit -\AppData\Local\Programs\Python\Python37 python.exe
Install Click In the virtual environment
If you are using a virtual environment, make sure you install the click module inside the virtual environment. Install it using the pip install command
# Create a virtual Environment
py -3 -m venv venv
# Activate the virtual environment (windows command)
venv\Scripts\activate.bat
# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1
# Activate the virtual environment (Linux)
source venv/bin/activate
# Install click inside the virtual environment
pip install click
Module Name not use as Variable Name
Make sure you never named any file as click as it can shadow the module click, which can lead to ModuleError.
Conclusion
I hope you find the answer to your query and know how to efficiently design a Python program without ModuleNotFoundError: No module named ‘click’ error.
Leave a comment