Error: No module named cv2 - python

I have already found few questions at SO but i am unable to solve this problem using the answers there.
I am new to python. I am having python in Ubuntu 12.04. In my /usr/local/lib, there are two python folders python 2.7 and python 3.2. python 2.7 contains dist-packages and site-packages while python 3.2 contains only dist-packages.
I am trying to run a very simple opencv example with the following code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image.JPG')
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
Error: No module named cv2

NB : This answer is a short compilation of comments above. For more details, please refer to the comments below question.
Background : OP is using SPE Stani's python editor. OP has installed OpenCV /opt/ros/hydro/lib/python2.7/dist-packages which is not detected by the above mentioned editor. Adding this path to PYTHONPATH doesn't solve the issue.
Solution (any one of the below):
Add this path to sys.path and put it in every file.
import sys
sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')
Copy cv2.so file to any directory in the sys.path.

I am able to resolve this problem of not loading cv2 dll by following below steps on my Win 64 machine.
Download the Unofficial Windows Binaries for Python Extension Packages from gohlke site.
In my case it is opencv_python-3.2.0-cp35-cp35m-win_amd64.whl downloaded it to some folder e.g. c:\py\lib
Install this .whl using below command
pip install c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl
Restart the kernel and error gone.

I had this issue and I fixed it by:
Finding where the openCV library was stored (did this through my IDE).
Deleting it.
Using "pip install opencv-python" again on the terminal in my IDE
Imported as normal
Hopefully this fixes other peoples issues too!

Related

Unresolved reference "cv2" inside cv2 (cv2.cv2)

I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all).
Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it.
So I went to the cv2 init file, which looks like this:
import importlib
from .cv2 import *
from .data import *
# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)
Pycharm detects an unresolved reference on the from .cv2 import * line and I imagine the same problem happens on the last line - I tried doing the following in a python console:
import cv2
print(__version__)
But I got a NameError, which seems to confirm my suspicion.
As I wrote, I have tried installing opencv-contrib-python but that didn't seem to do anything and frankly I'm already out of ideas.
Notes:
- I'm on Windows 10 x64.
- I'm using Python 3.6 x64.
- I have a virtual environment set up on my Pycharm project.
I'm no expert but the following line worked for me:
import cv2.cv2 as cv2
Everything seems to work afterwards. Autocompletion is also back
Have you installed opencv via terminal?
For example, like this.
$ pip install opencv-python
$ pip install opencv-contrib-python
I also experienced the same problem.
If you use pycharm, you should install opencv via pycharm.
File -> Settings... -> Project interpreter -> +
I got this same issue. You need to try a couple of things.
You need to simply import cv2 instead of cv2.cv2. Simply write "import cv2"
If you have installed any other library such as cv before, then uninstall it first.
The library that we need to install is opencv-python
You need to install it via IDE not with the terminal. Steps are as follows:
File -> Settings -> (Click on your project) -> Project Interpreter -> + -> (Type opencv-python) -> Download and install it -> It should work now.
From Python Interpreter download the version of opencv-python 4.5.4.60 and opencv-contrib-python 4.5.5.64.

'ImportError: No module named pillow' in PyCharm

I'm getting an error while using PyCharm which doesn't allow me to import the pillow module even though I have it installed as a package in the project interpreter. Any help is greatly appreciated!
http://imgur.com/a/DfjC3
While the name of the package is pillow, it is a replacement for PIL and uses the PIL for the name of the base module
the usual way to use pillow is
from PIL import Image
im = Image.open("filename")
See the tutorial, and the documentation
You try to run code with default Python interpreter (/Library/Frameworks/Python.framework/Versions/3.2/bin/python3). You need to configure PyCharm to run Your code with anaconda (~/anaconda/bin/python)
And now (Like #JamesK say) read Pillow tutorial and documentation:
import PIL not import Pillow
For anybody still having trouble with this, I did the following which solved my problem.
Open up your Project Interpreter (⌘ + , on Mac).
At the bottom of this page you'll see the + symbol to the left of the anaconda logo. This will create a pop-up that allows you to search for available packages.
In this new window, search for 'Pillow'.
Click and Install Package.
You should now be able to use "from PIL import Image" or "import Pillow as pil" etc.
After running this command on your terminal
pip install pillow
and you are sure it was installed, but still having same problem of PIL module not found.
Go to your IDE and make sure existing interpreter is set to python interpreter and not anaconda

PIL Image data can not convert to float

I am working through a few Python image tutorials. It seems that the greatest challenge every time I try something new in Python is just getting it to work! I have imported the PIL code and am trying to run the short snippet below in Spyder on Python 2.7.8
from PIL import Image
from pylab import *
im = array(Image.open('test.jpg'))
imshow(im)
show()
When I do so, I get "Image data can not convert to float" error.
I tried to do the same in an iPython notebook and received the same error. When I run pip freeze it shows that I have PIP 1.1.7 installed. Of note, I installed PIP using the executable at the host site, not with PIP. I am also using the Spyder instance that comes with Anaconda.
Any advice?
After trying other solutions posted I discovered that PIL may be out of date with my newer Python build (see link in comments). Once Pillow was installed the issue was resolved.

Trouble in installing Basemap in matplotlib

I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.

Image Module Python

I'm getting an error about a module and have searched for a solution for some time now and have come up empty handed. I'm coding in python and working in eclipse pydev. At the top of my code I typed.
import Image
I get a error that states ImportError: No module named Image
Here is just some screenshots of the Python Interpreter System Pythonpath
http://imageshack.us/a/img5/614/92989360.png
http://imageshack.us/f/545/79985417.png/
You need to install the Python Image Libray:
PIL
You can install it using pip via:
pip install PIL

Categories