Import Cv2 not working despite opencv installed - python

I have installed OpenCV with the command pip install opencv-contrib-python and when I run the command pip list I see opencv-contrib-python has been installed and yet import cv2 does not work for me and it throws
Traceback (most recent call last):
File "\[filename here\]", line 1, in \<module\>
import cv2
ImportError: No module named cv2
and I have tried uninstalling and reinstalling OpenCV and it still does not work. I tried different development environments like Pycharm and visual studio code. I also have my pip upgraded and my OpenCV is version 4.5.5 which is the latest version. I have tried using python 3.10 and 3.7 but the same error still shows. I have also tried using
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')
which was recommended in some answers and I have tried just installing opencv-python and using that and the same error still shows. I checked and I have cv2 in
my folder
but despite this, it keeps showing no module named cv2 every time I try to run import cv2
I use macOS Monterey version 12.2.1 and for my terminal, I use Darwan OS which is a Linux distro.
I tried:
pip install opencv-python
pip install opencv-contrib-python
putting this at the first lines of code:
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')

You have to use python2.7. As the manual says: "Currently, only the Python 2 version of the cv2 module is built and included in the latest release."
You can only use python3 if you manually build from source code.

Related

Cant install tensorflow for huggingface transformers library

Im trying to use huggingface transformers library in my python project. I am a first time python programmer, and I am stuck on this error message, even though tensorflow has been installed on my machine:
>>> from transformers import pipeline
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
I have discovered that tensorflow does not exist, even though I have installed it via pip. I have tried uninstalling it and reinstalling it, and but when I try to import the package, it just comes back as a ModuleNotFoundError
>>> import tensorflow
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import tensorflow
File "C:\Users\######\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
ModuleNotFoundError: No module named 'tensorflow.python
I have tried uninstalling and re-installing using pip and conda. I even tried installing pytorch using the same methods. It always says that the package was succesfully installed, and yet the error persists.
I am using Python 3.9 and my OS is Windows 10. I dont know what I am doing wrong. But I know that a solution will definitely not be to uninstall and reinstall a package.
Pip version (pip -V):
pip 21.1.3 from c:\users\######\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)
Python version (python -V):
Python 3.9.5
Python path list
I tried comparing the output of sys.path with the output of pip -V.
The closest path I saw for the pip -V path is down at the bottom, however I did not find the exact directory.
>>> import sys
>>> sys.path
['', 'C:\\windows\\system32', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\\python39.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\\lib', 'C:\\Users\\######\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0', 'C:\\Users\\######\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\\lib\\site-packages']
Closest path:
C:\Users\######\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages
From comments
You have multiple python interpreters installed, that is why
installing stuff does not show in your python interpreter, use pip -V and compare it to the python version that appears in the interpreter. Remove one and use only one then your issue will be
resolved (paraphrased from Dr.Snoopy)

Why do I still get the "module not found" error even after having installed the package using pip install?

I am trying to create an image processing program using convolutions. I need the package scikit-image, specifically this:
from skimage.exposure import rescale_intensity
I have repeatedly installed scikit-image using pip install scikit-image in my terminal (Mac). I did this in the folder where my convolutions.py file is located (is this what is meant by the PYTHONPATH?). However, I always get an error message:
Traceback (most recent call last):
File "Convolutions.py", line 6, in <module>
from skimage.exposure import rescale_intensity
ImportError: No module named skimage.exposure
How do I solve the problem?
make sure you are installing the package on the same version of python which you are running. On a mac, python by default runs python-2.7, and the command python3 runs python-3.x. Also, pip by default installs packages to python-2.7. To install them on python3 try running
python3 -m pip install scikit-image
or simply
pip3 install scikit-image

ImportError: No module named scenedetect on Linux Ubuntu

I'm trying to use pyscenedetect library on python for videos but I get this error when using the python interface and when I use the command line interface I get the error "ModuleNotFoundError: No module named 'cv2'"
even though I believe I installed both correctly according to the documentations.
I have trying to look for different ways to import opencv for the second error but to no avail. As for the first error i can't find any answers to my problem.
import cv2
import numpy as numpy
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
If you have pip you can try
pip install opencv-python
If you have anaconoda, you can try
conda install -c conda-forge opencv
it's probable that you installed it on a different python installation in your PC.
To know where your python installation is you can launch python and:
import sys
sys.path
To get the list of everything you have installed you can:
pip freeze > installed_modules.txt
Try only running
import cv2
So that you can test it
I found the problem. As Ivan was pointing out, the problem was with openCV.
I used the following command:
sudo apt install python3-opencv

tensorflow python 3.7.2 import error with anaconda [duplicate]

I have installed Ancaconda3 and Tensorflow. When I try to import Tensorflow in python shell I receive the following error:
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
ImportError: numpy.core.multiarray failed to import
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "", line 980, in _find_and_load SystemError:
<class '_frozen_importlib._ModuleLockManager'> returned a result with
an error set ImportError: numpy.core._multiarray_umath failed to
import ImportError: numpy.core.umath failed to import
I am not sure what the problem is as numpy is installed on my system and can be successfully imported in python.
I am using Windows10.
I also had the same issue.
It got resloved once I upgraded the numpy from 1.15.4 to 1.16.1.
If you're using pip:
pip install numpy --upgrade
Numpy that came with Anaconda3 is of version 1.15.4. so i upgraded and it worked.
Side note: if you're also using scikit-image in your script, be aware that numpy 1.16.3 has a conflict with old versions of scikit-image (e.g. you may get ImportError: cannot import name '_validate_lengths'). In that case, pip install --upgrade scikit-image from terminal solved the issue for me.
Kindly check whether you have installed the numpy package from pip. Because if you are running on conda evironment, then all packages need to be downloaded from there.
Please use the below mentioned statement for this purpose
conda install -c anaconda numpy
Also make sure that the numpy version supports the Python version you are using.
You can use two options in python 3.6
Install
py pip -m install numpy==1.14.5
Upgrade
py pip install numpy --upgrade
Note: the version most recently is 1.14.5
I also had this issue with python 3.8.9 and Numpy 1.24.1.
Downgrading to Numpy 1.21.0 fixed the issue.

Why can't I import opencv3 even though the package is installed?

I currently am running Python 3.5 and using Spyder from Anaconda as my IDE. I am running this on a Windows machine.
When I write import cv3 at the top of my code, it returns the error ImportError: No module named 'cv3'
I attempted to install opencv3 again with the command conda install -c https://conda.binstar.org/menpo opencv3 in the Command Prompt. It is apparently already installed because it returned
Fetching package metabase...............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at C:\Users\Joey\Anaconda3:
# opencv3 3.1.0 py35_0 https://conda.binstar.org/menpo
Am I importing cv3 wrong? How do I fix this error?
Update: Tried import cv3 instead of import cv2 but got the following error: ImportError: cannot import name 'cv2'. The wording on the two errors is different, so python must acknowledge there is opencv installed but it does not work for some reason. Any ideas?
Ironically enough, the module is still called cv2 because it doesn't represent the version of opencv but the actual C++ API underneath which is, to be contrasted from the C API, named - cv2... So try with: import cv2
Problem solved by using command pip uninstall opencv-python in the Command Prompt.
I have attempted several installations of opencv and I suppose one may have downloaded badly and Anaconda was attempting to read that one. I looked into the build of some of the other installations I attempted and some were for Python 2.7. Maybe that contributed to the error.
Thankfully, this worked. Now import cv2 works perfectly. No errors.
I used the same approach to install the package. However, I could not import the library using the name opencv3. I had to use cv2 which worked for me.
Elaborating on #zwer's answer, check the version of OpenCV after import cv2.
>>> cv2.__version__
'3.1.0'
So basically it's calling the OpenCV3 library.

Categories