Cannot find module cv2 even if it's installed - python

I encountered a problem where I have installed opencv in my anaconda environment using this command:
pip install opencv-contrib-python
But for some reason when I enter into my environment using python and error occurs stating: 'No module named cv2', has someone encountered a similar problem, thanks.

To use cv2, you need to install opencv-python, not the contrib. You can do it by pip install opencv-python.

Related

module 'cv2' has no attribute 'createThinPlateSplineShapeTransformer'

I'm trying to use the Thin plate shape transformer function.
tps = cv2.createThinPlateSplineShapeTransformer()
However, this error is shown:
module 'cv2' has no attribute 'createThinPlateSplineShapeTransformer'.
I have installed contrib using:
pip install opencv-contrib-python
but the problem is still unsolved. I use opencv 4.5.4 and python 3.8.5
See this issue: https://github.com/opencv/opencv-python/issues/177
It appears that that method has been moved out of the base package. So, you need to install OpenCV with contrib modules. For details, please open the link above.
I fixed this problem using the following commands.
pip uninstall opencv-contrib-python
pip install opencv-contrib-python

No module named 'cv2' even after successfully installing it using pip

I am working on ssd - tensor flow project. For which I have to use cv2.
I am able to import it in Jupyter notebook as well as in terminal too.
On running the following command
print cv2.__version__
I am getting output:-
3.4.1
which surely means its installed successfully. (I used sudo pip install opencv-python command to install and had tried brew install as well to resolve the issue) , but still when I run my example.py files using terminal commands. they are giving the following error
No module named 'cv2
I had tried every possibility (by searching for every related issue but still not able to resolve it). Please help.
This worked for me:
sudo apt-get install libopencv-dev python-opencv

AttributeError: module 'cv2' has no attribute 'imread'

(i'm on mac os 10.8.5)
I'm using Python 3 (through jupyter notebook) and trying to import cv2
I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5eb2880672d2> in <module>()
----> 1 im_g = cv2.imread("smallgray.png", 0)
AttributeError: module 'cv2' has no attribute 'imread'
I also checked dir(cv2) and I get:
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'
I guess a lot of functions are missing...
Is it due to a wrong opencv installation ?
Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal. (brew, pip)
Should I uninstall opencv and start over ? How can I do this properly ?
Thx in advance
It might be the case that you installed it in the wrong way. In my case as well, I installed OpenCV wrongly so I uninstalled it completely then reinstalled it which caused it to work.
Please notice the sequence of uninstalling and installing:
Uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
Install:
pip install opencv-contrib-python
pip install opencv-python
I was have this problem too, and i solved it, the problem was been in i named my script cv2.py, i'm just rename it to something else.
Reader's problem could be, that a wrong library (cv2 package) has been installed.
I installed opencv-python3 instead of opencv-python for example.
So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:
Make sure, you have installed opencv-python.
If you have also other similar names, it should be fine - in my case it works.
This might happen if you have named one of your files as 'cv2.py'. So when you 'import cv2', the system accesses your file instead of the actual library. So try renaming cv2.py' to any other name and your code should work fine.
I had the same issue, this turned out to solve the problem:
from cv2 import cv2
im_g=cv2.imread("smallgray.png", 0)
print(im_g)
Check:
brew list | grep opencv
If it doesn't installed then try:
brew install opencv
it works with me:
pip install opencv-python
pip install opencv-contrib-python
source : https://pypi.org/project/opencv-python/
Do cv2.cv2.imread()
This should work in most of the cases. Also, take a look here.
I faced Similar issue,On Ubuntu
I had installed open-cv using the comand:
sudo pip3 install opencv-python3
I Uninstalled opencv, By:
sudo pip3 uninstall opencv-python3
Then reinstalled it Using the following Code:
pip3 install opencv-python
I also faced this problem.
And get a solution by changing the current cv2.py to cv.py(You can name whatever you like except the name of packages in use).
Little Explanations
Saving the current working file as the name of cv2.py, when we try to run the file it imports the current file with import cv2 statement, not actual cv2 module. This whole thing is causing this whole Error.
The main problem is that you have your file or folder named "cv2", it is not permitted in Python. The way to solve is change the name and then your code will be run
The same issue has recently appeared within my code and had no idea of what's happening. Tried different methods such as uninstalling opencv-python.
Coming to the naming of the file that I am working with, I definitely had no problem that might cause a collapse with the name of the library.
I have deleted the opencv-python, cleaned all of the package-related caches, and then reinstalled the opencv-python and opencv-contrib-python. Now, it is working fine and I can say that no issues have appeared yet.
P.S I am using Arch Linux. If you wonder about how to clear caches, try looking at this source. I hope it helps. Keep learning and exploring, thanks!
I personally had to uninstall all previous installations dealing with opencv :
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
Then I deleted all files that had to do with opencv too in site-package
after the uninstallation, files related to opencv like opencv_contrib_python-4.6.0.66.dist-info remained and caused errors.
Once all these files were deleted I reinstalled opencv:
pip install opencv-python
now it works for me
using cv2 on vscode
i was having this problem but nothing worked. Turns out that i named my file as "cv2.py" so it was importing itself
"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error."
Perhaps you have just installed opencv-contrib-python and not opencv-python.
Try this out, it worked for me-
First uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
then install:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
You can refer here.

Unable to install cv2 on windows

I am trying to install opencv in python on my windows machine but I am unable to do so. I have python 2.7.11::Anaconda 2.4.1 <32-bit>
Here is what I have tried till now -
pip install cv2 on command line gives the error :
could not find a
version that satisfies the requirement cv2
I downloaded the package from sourceforge site, followed the steps
and pasted cv2.pyd in C:\Python27\Lib\site-packages but still it is
not working. I get the following error message
ImportError: No
module named cv2
(I already have numpy installed and it works just fine).
pip install opencv-python
you can type this instead of
pip install cv2
it also works with anaconda pro
I was able to solve the error.
If you are using python version 3 , sometimes you have to use pip3.
pip3 install opencv-python
Make sure you are using python 3 , it won't work for python 2.
Try
pip install opencv-python==4.3.0.36
So I was using PyCharm, and what worked for me was to install it directly from file->settings, Project:your-project-name->Python Interpreter list

Python: PIL _imaging C module

I'm trying to use a file that uses PIL and when I try to run it I get the following error:
ImportError: The _imaging C module is not installed
I know theres a bunch of threads online about this but most of them see pretty specific. I'm 100% sure there is no problem with the code I'm running. Python version 2.7.2 64bit windows 7. I've been trying to fix it for almost an hour or so and I'm losing my mind. Any suggestions?
try to install pillow. you can install it with the command: pip install pillow
you have install the python-imaging??
sudo apt-get install python-imaging. install first the python-imaging and next install the pillow
The error you're receiving suggests that the C library required by PIL is either not installed on your machine, or the directory in which it's installed is not on your path.
I would follow the instructions here: http://www.pythonware.com/products/pil/faq.htm
Search your system for _imaging.pyd
Check that the location of _imaging.pyd is on your path
Attempt to import the library from the command line with no accompanying code
If all of that works, please post your code, there is another problem.
On Ubuntu, the following command helped me (thanks to this answer on askubuntu):
sudo apt-get install libjpeg62:i386
I am on Windows, and had a problem ""ImportError: The _imaging C module is not installed"".
The problem solved by installing Pillow from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil. (it's given by a post however I can't locate it back..)

Categories

Resources