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

(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.

Related

how i can i solve no module named can error?

hello i'm using python 3 and ros noetic, and i got this error
import can
ImportError: No module named can
I've got this error before, and I solved it through a very simple can-bus related installation command in google. But I can't find that command now
I've tried all the like $ sudo apt install python3-can.
but I can't fix it at all
thank you................
The problem is, that the module can't be found by your python.
first, try to remove the package with:
pip uninstall python-can
and re-install it with
pip install python-can
In case you have several versions of python installed (such as python2 and python3) make sure, you use
pip3
instead of pip.
Next you can try to manually search your package directories for the package, if it is even there.
Try cloning the library with git and running the setup.py installation, worked for me.

Module PIL has not attribute "Resampling"

I have run the same code(with packages I needed) before and it worked, not sure what's happening now. This show the error,
AttributeError: module 'PIL.Image' has no attribute 'Resampling'. Probably it's small issue, but I can't figure it out, I am working in databricks.
I had the same problem. The easy way is use the old version of Pillow.
pip install Pillow==9.0.0
And your code should work.
Note: You can also use
pip install --ignore-installed Pillow==9.0.0
If for some reason, pip is refusing to install it. Note, however, that it can break dependencies, so use it only as a last resort.
I had the same problem and found that I need to replace PIL.Image.Resampling.BICUBIC with PIL.Image.BICUBIC.
I am using pillow version 7.1.2
from PIL import Image
im = Image.open('image.png')
im2 = im.resize((512,512),resample=Image.BICUBIC)
display(im2)
The resampling enums were seemingly added in Pillow 9.1.0 (released 3 days ago) via this pull request.
I would imagine your Databricks environment has a different verison.
I find that forcing the use of a particular Pillow version may break other packages.
Another solution that works for any Pillow version and avoids deprecation warnings is to insert the code:
import PIL.Image
if not hasattr(PIL.Image, 'Resampling'): # Pillow<9.0
PIL.Image.Resampling = PIL.Image
# Now PIL.Image.Resampling.BICUBIC is always recognized.
my way to fix it:
pip install --ignore-installed Pillow==9.3.0
Same happened when I upgraded some module. I just restarted runtime and it helped.
In my case, pip install Pillow==9.1.0 is suitable for my code

Cannot find module cv2 even if it's installed

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.

No module named 'structure' when installing PyBrain even though it's in the folder

I installed PyBrain via PyCharm and when I try to compile I get the following error:
Traceback (most recent call last):
File "C:/Users/Marcus/PycharmProjects/ANN/ann.py", line 2, in <module>
from pybrain.tools.shortcuts import buildNetwork
File "C:\Python34\lib\site-packages\pybrain\__init__.py", line 1, in <module>
from structure.__init__ import *
ImportError: No module named 'structure'
However, in the folder site-packages\pybrain\ there's a folder named 'structure' with (what I assume is) what pybrain is asking for.
I'm pretty new to Python so I'm not completely used to this installing packages thing. Is it something that I missed? I have tried to search for a package named "structure" in the PyCharm package installer but there is none (only a 'structures').
Thanks in advance
EDIT: A comment asked me to further explain how I installed the package. In PyCharm, I went to project settings -> project interpreter -> search for "pybrain"
I was having the same problem and tried all the suggestions mentioned here. Finally I realized that the version that was installing was 0.3.1, and it is not compatible with Python 3. So I deleted that version and then ran
!pip install https://github.com/pybrain/pybrain/archive/0.3.3.zip(from Spyder ipython console). This finally worked.
I encountered the same problem.
I am working on Windows 8, 64 bit machine, with WinPython.
As every newbie would do. I install pip and then installed pybrain (or PyBrain, doesn't make difference).
After I tried to see if its working like this :
import pybrain
I got an error saying :
There is no module named "Structures".
When I checked the site-packages, it was there.
I tried the following approaches :
I tried reinstalling Structures.
I tried using pip3 instead of pip.
I tried pip3 install pybrain --upgrade
I downloaded the .zip file of the entire project from github here and after unzipping it, went inside it from cmd and did python setup.py install.
The 4th approach worked like a charm.
Cheers.
What I have done as following and it works. (windows 7, anaconda3 installed)
Download ZIP file from https://github.com/pybrain/pybrain
Extract the zip file and try python setup.py install
Open anaconda command and run conda update conda
This worked for me: pip3 install https://github.com/pybrain/pybrain/archive/0.3.3.zip
In my case was pip3, but it could be pip
I would use pip (installed with 3.4) to install packages for 3.4+. I used pip install pybrain (or maybe ... PyBrain) last summer (for someone else) and the install worked with no problem.
What is a problem is that PyBrain is a 2.x package. Fortunately, most of the incompatibilities are in the test suite (and those mostly due to using doctests). The user I installed it for ran into a few easily fixed syntax errors and is still using PyBrain for a research project.

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