# "ModuleNotFoundError: No module named 'cv2' in Python"
The Python "ModuleNotFoundError: No module named 'cv2'" occurs when we forgetto install the opencv-python
module before importing it or install it in anincorrect environment.
To solve the error, install the module by running thepip install opencv-python
command.
Open your terminal in your project's root directory and install theopencv-python
module.
shell
Copied!
# 👇️ In a virtual environment or using Python 2pip install opencv-python# 👇️ For Python 3 (could also be pip3.10 depending on your version)pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-pythonpip install opencv-python --user# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3 (could also be pip3.10 depending on your version)python3 -m pip install opencv-python# 👇️ Using py alias (Windows)py -m pip install opencv-python# 👇️ For Anacondaconda install -c conda-forge opencv# 👇️ For Jupyter Notebook!pip install opencv-python
After you install the opencv-pythonpackage, try importing it like:
main.py
Copied!
import cv2print(cv2.__version__)
# Common causes of the error
The error occurs for multiple reasons:
- Not having the
opencv-python
package installed by runningpip install opencv-python
. - Installing the package in a different Python version than the one you'reusing.
- Installing the package globally and not in your virtual environment.
- Your IDE running an incorrect version of Python.
- Naming your module
cv2.py
which would shadow the official module. - Declaring a variable named
cv2
which would shadow the imported variable.
If the error persists, get your Python version and make sure you are installingthe package using the correct Python version.
shell
Copied!
python --version
For example, my Python version is 3.10.4
, so I would install the opencv-pythonpackage with pip3.10 install opencv-python
.
shell
Copied!
pip3.10 install opencv-python# 👇️ If you get a permissions error use pip3 (NOT pip3.X)sudo pip3 install opencv-python
Notice that the version number corresponds to the version of pip
I'm using.
If the PATH for pip
is not set up on your machine, replace pip
withpython3 -m pip
:
shell
Copied!
# 👇️ Make sure to use your version of Python, e.g. 3.10python3 -m pip install opencv-python
If the error persists,try restarting your IDE and developmentserver/script.
# Check if the package is installed
You cancheck if you have the opencv-python package installedby running the pip show opencv-python
command.
shell
Copied!
# 👇️ Check if you have opencv-python installedpip show opencv-python# 👇️ If you don't have pip set up in PATHpython -m pip show opencv-python
The pip show opencv-python
command will either state that the package is notinstalled or show a bunch of information about the package, including thelocation where the package is installed.
# Make sure your IDE is using the correct Python version
If the package is not installed, make sure your IDE isusing the correct version of Python.
If you have multiple Python versions installed on your machine, you might have installed the opencv-python
package using the incorrect version or your IDE might be set up to use a different version.
For example, In VSCode, you can press CTRL + Shift + P
or (⌘
+ Shift
+ P
on Mac) to open the command palette.
Then type "Python select interpreter" in the field.
Then Select the correct Python version from the dropdown menu.
Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.
# Install the package in a Virtual Environment
If you are using a virtual environment, make sure you are installingopencv-python
in your virtual environment and not globally.
You can try creating a virtual environment if you don't already have one.
shell
Copied!
# 👇️ Use the correct version of Python when creating VENVpython3 -m venv venv# 👇️ Activate on Unix or MacOSsource venv/bin/activate# 👇️ Activate on Windows (cmd.exe)venv\Scripts\activate.bat# 👇️ Activate on Windows (PowerShell)venv\Scripts\Activate.ps1# 👇️ Install opencv-python in your virtual environmentpip install opencv-python
If the python3 -m venv venv
command doesn't work, try the following 2commands:
python -m venv venv
py -m venv venv
Your virtual environment will use the version of Python that was used to createit.
If the error persists, make sure you haven't named a module in your project as cv2.py
because that would shadow the original opencv-python
module.
You also shouldn't be declaring a variable named opencv-python
as that wouldalso shadow the original module.
# Try reinstalling the package
If the error is not resolved, try to uninstall the opencv-python
package andthen install it.
shell
Copied!
# 👇️ Check if you have opencv-python installedpip show opencv-python# 👇️ If you don't have pip set up in PATHpython -m pip show opencv-python# 👇️ Uninstall opencv-pythonpip uninstall opencv-python# 👇️ If you don't have pip set up in PATHpython -m pip uninstall opencv-python# 👇️ Install opencv-pythonpip install opencv-python# 👇️ If you don't have pip set up in PATHpython -m pip install opencv-python
Try restarting your IDE and development server/script.
You can also try to upgrade the version of the opencv-python package.
shell
Copied!
pip install opencv-python --upgrade# 👇️ If you don't have pip set up in PATHpython -m pip install opencv-python --upgrade
# Import "cv2" could not be resolved from source Pylance
The errorImport "cv2" could not be resolved from source Pylanceoccurs when the opencv-python
module is not installed or you have selected theincorrect Python interpreter in your IDE (e.g. Visual Studio Code).
To solve the error, install opencv-python
and select the correct Pythoninterpreter in your IDE.
shell
Copied!
Import "cv2" could not be resolved from source Pylance(reportMissingModuleSource) [Ln 1, Col 8]
# Make sure the correct Python interpreter is selected in your IDE
If you have multiple Python versions installed on your machine, you might haveinstalled the opencv-python
package using the incorrect version or your IDEmight be set up to use a different version.
For example, In Visual Studio Code you can:
- Press
CTRL + Shift + P
or (⌘
+Shift
+P
on macOS) to open thecommand palette.
Then type "Python select interpreter" in the search field.
- Select the correct Python version from the dropdown menu.
Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.
If the error persists, try restarting your IDE and development server/script.VSCode often glitches and a reboot resolves the issue.
If the error is not resolved, try to use the Visual Studio Code terminal toinstall the opencv-python
module.
You can press CTRL + ` (Backtick) on your keyboard to open the Visual Studiocode terminal.
You can also open the terminal in Visual Studio Code by pressing CTRL+Shift+P
and then type "View: Toggle Terminal".
Once you open the terminal, Visual Studio Code will automatically activate yourvirtual environment (if you have one).
Run the pip install opencv-python
command.
shell
Copied!
pip install opencv-python
If the error persists, try to select the Python interpreter by specifying thepath:
- Press
CTRL + Shift + P
or (⌘
+Shift
+P
on Mac) to open the commandpalette. - Type "Python select interpreter" in the field.
- Select "Enter interpreter path...".
- Click on "Find".
- In the window that opens, navigate to your Python executable:
- If you have a virtual environment on Windows, click on your
venv
folder,then double-click on theScripts
folder, select thepython.exe
file andthen Select interpreter.
- If you have a virtual environment on macOS or Linux, click on your
venv
folder, then double-click on thebin
folder, select thepython
file andthen Select interpreter.
- If you don't have a virtual environment, use one of the following commands toget your path to your
python.exe
orpython
executable, specify the path tothe file and select the executable.
cmd
Copied!
where pythonpython -c "import sys; print(sys.executable)"
If the error persists, try restarting your IDE and development server/script.
# Alternatively, use a comment to disable the warning
If none of the suggestions helped, you can use a comment to disable the Pylancewarning in your IDE.
main.py
Copied!
import cv2 # type: ignoreprint(cv2)
You simply have to add the # type: ignore
command on the same line as theimport statement to disable the check for the specific import.
If the error persists, follow the operating system-specific instructions on how to install opencv-python
.
# Table of Contents
- Install opencv-python (cv2) on Windows
- Install opencv-python (cv2) on macOS or Linux
- Install opencv-python (cv2) in Visual Studio Code
- Install opencv-python (cv2) in PyCharm
- Install opencv-python (cv2) in Anaconda
- Install opencv-python (cv2) in Jupyter Notebook
# Install opencv-python (cv2) on Windows
To install the opencv-python
module on Windows:
- Type CMD in the search bar and open the Command Prompt application.
- Type
pip install opencv-python
and press Enter.
cmd
Copied!
pip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Using py aliaspy -m pip install opencv-python# 👇️ If you get a permissions errorpip install opencv-python --user# 👇️ For Anacondaconda install -c conda-forge opencv
If the command doesn't succeed, try running CMD as an administrator.
Right-click on the search result, click on "Run as administrator" and run the pip install command.
If you get the error'pip' is not recognized as an internal or external command,use the python -m
command when installing opencv-python
.
shell
Copied!
python -m pip install opencv-pythonpython3 -m pip install opencv-pythonpy -m pip install opencv-python
Alternatively, you can install the opencv-python
module in a virtualenvironment:
- Open the root directory of your project.
- Press
Shift
and right-click in Explorer.
- Click on "Open PowerShell window here".
- Run the following commands.
PowerShell
Copied!
# 👇️ Might also be: "python3 -m venv venv"python -m venv venv# 👇️ Activate on Windows (PowerShell)venv\Scripts\Activate.ps1# 👇️ Activate on Windows (cmd.exe)venv\Scripts\activate.bat# 👇️ Install opencv-python in your virtual environmentpip install opencv-python
If the python -m venv venv
command doesn't work, try the following 2 commands:
python3 -m venv venv
py -m venv venv
.
If you see an error message thatps1 cannot be loaded because running scripts is disabled on this system,run the following command, type "yes" when prompted and rerun the activationcommand.
PowerShell
Copied!
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
You can verify that the opencv-python
module is installed by using the pip show opencv-python
command.
PowerShell
Copied!
pip show opencv-pythonpip3 show opencv-pythonpython -m pip show opencv-pythonpython3 -m pip show opencv-python
The pip show opencv-python
command will either state that the package is notinstalled or show a bunch of information about the package, including thelocation where the package is installed.
# Install opencv-python (cv2) on macOS or Linux
To install opencv-python on macOS or Linux:
- Search for "terminal" and start the application.
- Type
pip install opencv-python
and press Enter.
terminal
Copied!
pip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Alternative if you get a permissions errorpip install opencv-python --user# 👇️ For Anacondaconda install -c conda-forge opencv
If you get an error that pip
isn't found, use the python -m
command.
terminal
Copied!
python -m pip install opencv-pythonpython3 -m pip install opencv-python
If you get a permissions error, prefix the command with sudo
.
terminal
Copied!
sudo pip install opencv-pythonsudo pip3 install opencv-python
Alternatively, you can install the opencv-python
package in a virtualenvironment:
- Open your terminal in the root directory of your project.
- Run the following commands.
shell
Copied!
# 👇️ Could also be "python -m venv venv"python3 -m venv venv# 👇️ Activate virtual env on macOS or Linuxsource venv/bin/activate# 👇️ Install opencv-python in your virtual environmentpip install opencv-python
Your virtual environment will use the version of Python that was used to createit.
If the python3 -m venv venv
command doesn't work, use python -m venv venv
instead.
You can use the pip show
command to verifyopencv-python has been installedsuccessfully.
shell
Copied!
pip show opencv-pythonpip3 show opencv-pythonpython -m pip show opencv-pythonpython3 -m pip show opencv-python
The pip show opencv-python
command will either state that the package is notinstalled or show a bunch of information about the package.
# Install opencv-python (cv2) in Visual Studio Code
To install opencv-python in Visual Studio Code:
- Press CTRL + ` (Backtick) on your keyboard to open the terminal.
- Run the
pip install opencv-python
command to install theopencv-python
module.
terminal
Copied!
pip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Using py aliaspy -m pip install opencv-python# 👇️ Alternative if you get a permissions errorpip install opencv-python --user
You can also open the terminal in Visual Studio Code by pressing CTRL+Shift+P
and then type "View: Toggle Terminal".
When installing Python modules in Visual Studio code, make sure that your IDE isconfigured touse the correct version of Python.
Press CTRL+Shift+P
or (⌘
+ Shift
+ P
on Mac) to open the commandpalette.
Then type "Python select interpreter" in the field.
Then Select the correct Python version from the dropdown menu.
Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.
You can use the python --version
command if you need to get your version ofPython.
terminal
Copied!
python --versionpython3 --version
You can also try creating a virtual environment if you don't already have one.
terminal
Copied!
# 👇️ Could also be "python -m venv venv" or "py -m venv venv"python3 -m venv venv# 👇️ Activate on Unix or MacOSsource venv/bin/activate# 👇️ Activate on Windows (cmd.exe)venv\Scripts\activate.bat# 👇️ Activate on Windows (PowerShell)venv\Scripts\Activate.ps1# 👇️ Install opencv-python in your virtual environmentpip install opencv-python
Your virtual environment will use the version of Python that was used to createit.
# Install opencv-python (cv2) in PyCharm
To install opencv-python in PyCharm:
- Press
Alt+F12
on your keyboard to open the terminal. - Run the
pip install opencv-python
command to install theopencv-python
module.
terminal
Copied!
pip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Using py aliaspy -m pip install opencv-python# 👇️ Alternative if you get a permissions errorpip install opencv-python --user
Alternatively, you can use the IDE itself to install the module.
- Click on "File" > "Settings" > "Project" > "Python Interpreter".
- Click on the
+
icon and typeopencv-python
. - Click on "Install Package".
When installing Python modules in PyCharm, make sure that your IDE is configured to use the correct version of Python.
Click on "File" > "Settings" > "Project" > "Python Interpreter".
Then Select the correct Python version from the dropdown menu.
Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.
You can use the python --version
command if you need to get your version ofPython.
terminal
Copied!
python --versionpython3 --version
# Install opencv-python (cv2) in Anaconda
To install opencv-python in Anaconda:
- Open your Anaconda Navigator.
- Click on "Environments" and select your project.
- Type
opencv
in the search bar to the right. - Tick the
opencv
package and click on "Apply".
Alternatively, you can install the opencv-python
package with a command.
If you are on Windows, search for "Anaconda Prompt" and open theapplication.
If you are on macOS or Linux, open your terminal.
Run the following command to install the opencv-python
package.
shell
Copied!
# 👇️ Using condaconda install -c conda-forge opencv# 👇️ Alternatively use `pip`pip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Using py aliaspy -m pip install opencv-python# 👇️ Alternative if you get a permissions errorpip install opencv-python --user
Click on thefollowing articleif you need to install a specific version of the package using Anaconda.
# Install opencv-python (cv2) in Jupyter Notebook
To install opencv-python in Jupyter Notebook:
- Open your terminal and type "jupyter notebook".
- Click on "New" and then "Terminal" in the browser tab.
- Type
pip install opencv-python
and press Enter.
shell
Copied!
# 👇️ Using pippip install opencv-python# 👇️ For Python 3pip3 install opencv-python# 👇️ If you get a permissions errorsudo pip3 install opencv-python# 👇️ If you don't have pip in your PATH environment variablepython -m pip install opencv-python# 👇️ For Python 3python3 -m pip install opencv-python# 👇️ Using py aliaspy -m pip install opencv-python# 👇️ Using condaconda install -c conda-forge opencv# 👇️ Alternative if you get a permissions errorpip install opencv-python --user
Alternatively, you can use the Python ipykernel.
- Open your terminal and type "jupyter notebook".
Click on "New" and then click on "Python 3 (ipykernel)".
Type
!pip install opencv-python
and click on "Run".
Note that the pip install
command must be prefixed with an exclamation mark ifyou use this approach.
shell
Copied!
!pip install opencv-python
Once you type the command, click "Run" to install the opencv-python
module.
If you get a permissions error, e.g. "[WinError: 5] Access is denied", add the--user
option to the installation command.
shell
Copied!
!pip install opencv-python --user
If the error persists, try torestart the Jupyter Kerneland rerun the command.