Search code examples
python-3.6importerroropencvpython

Import cv2 doesn't work despite having installed OpenCV-python


I am a newbie and have been trying to run a python code that uses cv2. I am using the python 3.6.8 terminal. Please see below for the first part of the code:

import cv2
import numpy as np
import os
import sys
import tensorflow as tf

from sklearn.model_selection import train_test_split

Although I have installed opencv-python, I get a "Module not found" error message as below:

(base) Dhammas-MacBook-Air:python-code-r dhammaandrivini$ python3 traffic.py
Traceback (most recent call last):
File "traffic.py", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'

I would be most grateful for any help with this, as I really want to finish this project! :)


Solution

  • It is most likely that you have installed the package using pip install opencv-python. This is the correct command but if you have multiple python installations on your device it might lead to issues.

    I would suggest you try and use the command python3 -m pip install opencv-python, this command will install opencv for the pip being used by python3, which you seem to be using to run your code.