pip安装了python3版本的opencv3却仍然调用系统之前安装的python2版本的opencv3

我的系统自带通过python2编译的opencv3,然后我使用conda创建了python3版本环境名称为py3,使用pip install opencv-python安装了python3版本的opencv,

但是在py3环境里使用import cv2不能正常导入python3版本的opencv而是使用系统自带的python2版本的opencv

我的~/.bashrc里已经添加了conda,然后激活我安装的python3版本的opencv3版本的,如下所示:

export PATH=/home/jason/anaconda2/bin:$PATH
conda activate py3

然后source ~/.bashrc

然后import cv2时报错如下:

(py3) [hadoop-perception@zw01-data-hdp-dn-gpu43 cephfs]$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) 
[GCC 7.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>
  File "/opt/devel/opencv/lib/python2.7/site-packages/cv2/__init__.py", line 89, in <module>
    bootstrap()
  File "/opt/devel/opencv/lib/python2.7/site-packages/cv2/__init__.py", line 62, in bootstrap
    ], True)
  File "/opt/devel/opencv/lib/python2.7/site-packages/cv2/__init__.py", line 56, in load_first_config
    raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config-3.6.py', 'config-3.py']. Check OpenCV installation.
>>> exit()

由于系统之前使用python2.7编译了opencv,并且将动态库路径添加到了/etc/ld.so.conf中,内容如下:

include ld.so.conf.d/*.conf

/opt/devel/opencv/lib64

由于该文件有root权限无法更改,

编译好的opencv位于/opt/devel/opencv,该文件包含bin include lib lib64 share五个文件,include下有opencv 和opencv2两个文件,大部分的.so文件位于lib64,python2编译的cv2.so位于lib,其中cv2.so具体位于/opt/devel/opencv/lib/python2.7/site-packages/cv2/python-2.7/cv2.so,import cv2就是import的这个.so文件,因此我需要让系统找到我在py3环境里的cv2.so

 

解决问题:

1,尝试一(未成功)

我尝试在bashrc中修改我用pip安装的python3版本的opencv的动态库路径

vim ~/.bashrc

在里面加入

export LD_LIBRARY_PATH=/home/jason/anaconda2/envs/py3/lib/:$LD_LIBRARY_PATH

然后source ~/.bashrc

该方案仍然会报错,后来想到LD_LIBRARY_PATH动态库是对c、c++代码来说的,对于python来说应该使用PYTHONPATH来告诉系统import时应该去哪个文件下找

2,尝试二(成功)

1)使用PYTHONPATH环境变量

然后我找到那个python3编译之后的cv2.so具体路径位于:

该so文件名字会根据编译的系统python的版本不同,架构等有关,如cv2.cpython-36m-x86_64-linux-gnu.so, 搜索这个文件路径可以cd /home/jason/anaconda2/envs/py3/lib, 然后使用find  ./ -iname "*cv*"用于搜索lib文件夹下含cv的文件所在路径,-iname忽略大小写,*为通配符,“是必须的

/home/jason/anaconda2/envs/py3/lib/python3.6/site-packages/cv2/cv2.cpython-36m-x86_64-linux-gnu.so

2)在~/.bashrc中加入:PYTHONPATH只需要包含cv2.cpython-36m-x86_64-linux-gnu.so的目录

export PYTHONPATH=/home/jason/anaconda2/envs/py3/lib/python3.6/site-packages/cv2:$PYTHONPATH

3)然后 source ~/.bashrc

最后我的bashrc中加入的内容如下,保留LD_LIBRARY_PATH的原因是想着以后可能需要找库文件可以找到:

export PATH=/home/jason/anaconda2/bin:$PATH
conda activate py3
export LD_LIBRARY_PATH=/home/jason/anaconda2/envs/py3/lib/:$LD_LIBRARY_PATH
export PYTHONPATH=/home/jason/anaconda2/envs/py3/lib/python3.6/site-packages/cv2:$PYTHONPATH

4)此时import cv2已经不报上面的错误了,报的错误如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libSM.so.6: cannot open shared object file: No such file or directory

centos下安装libSM,

yum install libSM

ubuntu下可以查看我的这篇文章https://blog.csdn.net/u014734886/article/details/93029349 解决caffe编译时//usr/lib/x86_64-linux-gnu/libSM.so.6: undefined reference to `uuid_unparse_lower@UUID_1.0

接着报错如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libXext.so.6: cannot open shared object file: No such file or directory

centos下安装libXext,

yum install libXext

ubuntu下安装libXext应该和前面安装libSM类似

至此成功

(py3) [hadoop-perception@zw01-data-hdp-dn-gpu43 ~]$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> exit()

 

 

GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐