简体   繁体   English

尝试在 python (Ubuntu 18.04) 中导入 cv2 时收到并导入错误

[英]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.几天前,我在 Ubuntu 18.04 桌面系统中安装了 Python3 (3.6.9) 和 opencv 3.4.10。 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:我需要在一些编译问题(与 python3 无关)之后将 opencv 降级到 3.4.0,所以我使用sudo make uninstall命令删除了 opencv 3.4.10,并测试它是否被卸载,就像我尝试在 python 中导入它一样得到以下错误:

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.我认为安装 3.4.0 版本可以解决问题,但现在它已安装,当我尝试在 python3 脚本中导入它时,我遇到了同样的错误。 I think this error must be related to the way python3 references the import files.我认为这个错误一定与python3引用导入文件的方式有关。 I think it's still pointing to the uninstalled version of 3.4.10, but I can not find the way to change it.我认为它仍然指向3.4.10的卸载版本,但我找不到更改它的方法。

Could someone please help me with this issue?有人可以帮我解决这个问题吗?

Thank you very much.非常感谢。

Andrés.安德烈斯。

Using a virtualenv would resolve the issue.使用 virtualenv 可以解决这个问题。 Check this out on how to use : Installing packages using pip and virtual environments查看如何使用: 使用 pip 和虚拟环境安装包

Once you're done installing requirements in the virtualenv, run your python script there only to use the specified versions.在 virtualenv 中完成安装要求后,在那里运行你的 python 脚本,只使用指定的版本。

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.还以为是卸载了之前版本的opencv,我的python虚拟环境路径下还有一个cv2.so文件,而且还没有被新的cv2.so替换,所以python试图导入旧的而不是新的一。

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 :)旧的cv2.so (我在 python 中导入 cv2 时引用的那个)就在这里 --> ~/.virtualenvs/cv/lib/python3.6/site-packages/cv2.so cv2.so我想要的cv2.so在这里导入时使用 --> /usr/local/lib/python3.6/site-packages/cv2/cv2.so所以我用 lib 目录中的一个替换了虚拟环境路径中的一个,然后诡计 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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