7

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

27
  • check if your dist_packages/ have cv2.so file in it? Commented Apr 13, 2014 at 8:15
  • @AbidRahmanK: i just checked, all of these folders are empty. What should i do now? Commented Apr 13, 2014 at 8:19
  • 1
    import cv2 print cv2.__file__ should return path of that file. For example, I got /usr/local/lib/python2.7/site-packages/cv2.so in my system. Commented Apr 13, 2014 at 8:51
  • 1
    Try this: import sys , sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages') , import cv2, print cv2.__version__ Commented Apr 13, 2014 at 8:55
  • 1
    import sys , print sys.path in SPE. If your cv2.so directory is not in the list, try copying the cv2.so to any of the directory in that list. Commented Apr 13, 2014 at 9:07

3 Answers 3

5

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):

  1. 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')

  1. Copy cv2.so file to any directory in the sys.path.
Sign up to request clarification or add additional context in comments.

Comments

0

I am able to resolve this problem of not loading cv2 dll by following below steps on my Win 64 machine.

  1. Download the Unofficial Windows Binaries for Python Extension Packages from gohlke site.
  2. 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
  3. Install this .whl using below command

    pip install c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl

  4. Restart the kernel and error gone.

Comments

0

I had this issue and I fixed it by:

  1. Finding where the openCV library was stored (did this through my IDE).
  2. Deleting it.
  3. Using "pip install opencv-python" again on the terminal in my IDE
  4. Imported as normal

Hopefully this fixes other peoples issues too!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.