简体   繁体   中英

I am receiving and import error when trying to import cv2 in python (Ubuntu 18.04)

First of all, thank you very much for reading.

Some days ago I installed Python3 (3.6.9) and opencv 3.4.10 in an Ubuntu 18.04 desktop system. I needed to downgrade opencv to 3.4.0 after some compilation problems (not related to python3), so I removed opencv 3.4.10 by using the sudo make uninstall order, and tested that it was uninstalled as if I tried importing it in python I got the following error:

libopencv_hfs.so.3.4: cannot open shared object file: no such file or directory

I thought installing 3.4.0 version would solve the problem but now that it's installed, when I try to import it in a python3 script I am getting the same error. I think this error must be related to the way python3 references the import files. I think it's still pointing to the uninstalled version of 3.4.10, but I can not find the way to change it.

Could someone please help me with this issue?

Thank you very much.

Andrés.

Using a virtualenv would resolve the issue. Check this out on how to use : Installing packages using pip and virtual environments

Once you're done installing requirements in the virtualenv, run your python script there only to use the specified versions.

Hello again and thank you for you answers.

I have been able to solve the problem. Thought I had uninstalled the previous version of opencv, there was still a cv2.so file in my python virtual environment path, and it had not been replaced by the new cv2.so , so python tried to import the old one instead of the new one.

The old cv2.so (the one that was referenced when I imported cv2 in python) was here --> ~/.virtualenvs/cv/lib/python3.6/site-packages/cv2.so The cv2.so I wanted to be used when imported was here --> /usr/local/lib/python3.6/site-packages/cv2/cv2.so so I replaced the one in the virtual environment path with the one in the lib directory and that did the trick :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM