我的系统自带通过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 "", line 1, in
File "/opt/devel/opencv/lib/python2.7/site-packages/cv2/__init__.py", line 89, in
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
我尝试在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时应该去哪个文件下找
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 "", line 1, in
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 "", line 1, in
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()