HobieKatz
Posts: 3
Joined: Mon Jan 08, 2018 1:54 am

Opencv 3.4.0 error: "No module named cv2"

Mon Jan 08, 2018 2:18 am

I'm trying to install Opencv 3.4.0 using python 2.7 and I think it installed correctly, although I am new to linux and don't know how to check, I didn't see any errors in the process, but it installed the cv2.so file in the usr/local/lib/python2.7/dist-packages instead of the site-packages folder and nothing happens, just another prompt '>>>' :

[email protected]:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

So then I moved the file and tried it again using the following code and I got the popular "No module named cv2" error. I think the file exists, I can see its moving, but I can't find a solution to this instance of the "No module named cv2" error.

[email protected]:~ $ sudo mv /usr/local/lib/python2.7/dist-packages/* /usr/local/lib/python2.7/site-packages/
[email protected]:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>>

Please help!
Hobie

HobieKatz
Posts: 3
Joined: Mon Jan 08, 2018 1:54 am

Re: Opencv 3.4.0 error: "No module named cv2"

Mon Jan 08, 2018 6:05 pm

I will answer my own question then.

First - The error "No module named cv2" was due to the cv2 file ending up in the dist-packges folder instead of the site-packages folder upon installation, so I copied it to the correct folder:
sudo cp /usr/local/lib/python2.7/dist-packages/cv2.so /usr/local/lib/python2.7/site-packages/cv2.so

I did the same thing with python 3.5 and it works with Opencv too.

Secondly - Apparently, version 3.4.0 does not have the part where it returns the version with the command
Import cv2, it doesn't do anything, so when I thought it was an error that it didn't return the version. I was wrong, that is the way it works.

for version 3.4.0, this means it worked, on my system anyway, which is a Rpi 3 running Stretch.

[email protected]:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

these tutorials are great for getting opencv compiled, and I thank them for their efforts, but they are not updated for this:
https://www.pyimagesearch.com/2017/09/0 ... ent-445865
https://github.com/MicrocontrollersAndM ... 0Sheet.txt


After copying the file to the site-packes folder in python 3.5, this works too.
[email protected]:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

Good luck!
-Hobie

HobieKatz
Posts: 3
Joined: Mon Jan 08, 2018 1:54 am

Re: Opencv 3.4.0 error: "No module named cv2"

Mon Jan 08, 2018 6:10 pm

Oh, and I forgot to mention that for Python 3.5 folder I had to rename the cv2 file, it had the wrong extension:
sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

-Hobie

Return to “Camera board”