4

If you aren't using a virtualenv, the cv2 module is imported successfully

nano@nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.1'
>>> 

However, if you create a virtual environment, cv2 is not found

(env) nano@nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
Jonathan Tse
  • 875
  • 1
  • 9
  • 14
  • OpenCV needs to be installed under virtualenv. See [this guide](https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/). – zindarod May 20 '19 at 15:55
  • Normally that would be correct, but this is not the right advice for the Jetson platform. Nvidia packages a custom-built version of OpenCV that takes advantage of GPU acceleration. Installing OpenCV this way will work, but you'll not be able to take full advantage of the hardware. – T3am5hark Dec 18 '20 at 20:15

3 Answers3

6

More specifically it seems you need to simlink opencv as:

first: cd into your_venv/lib/python3.6/site-packages

and:

ln -s /usr/lib/python3.6/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so cv2.cpython-36-m-aarch64-linux-gnu.so
  • For me this is the path: `/usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so` . You can do an individual search with this command: `find /usr/local -name "*opencv*" -o -name "*cv2*"` – Dániel Terbe Mar 18 '20 at 08:49
  • One more convenient thing: you can enter the activated virtualenv's site-package dictionary with this command: `cdsitepackages` – Dániel Terbe Mar 18 '20 at 09:27
1

You can copy the directory /usr/local/lib/python3.6/dist-packages/cv2/ to your virtual env folder, for example env/lib/python3.6/dist-packages/cv2/

aryan_461
  • 49
  • 1
  • 6
0

Either you can install opencv in virtual environment or can make symbolic link of library to vitual env in bashrc.

ln -s source_file destination_file
adiga
  • 34,372
  • 9
  • 61
  • 83
Anchal Gupta
  • 219
  • 1
  • 9