问题描述

在python3.5环境中导入cv2报错,在python2.7中正常。注:命令行的前缀RL_2018HW是python3.5的环境

(RL_2018HW) gordon@gordon-:$ python
Python 3.5. (default, Nov , ::)
[GCC 5.4. ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line , in <module>
ImportError: /opt/ros/kinetic/lib/python2./dist-packages/cv2.so: undefined symbol: PyCObject_Type gordon@gordon-:$ python
Python 2.7. (default, Dec , ::)
[GCC 5.4. ] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2

从报错可以看出,在python3.5虚拟环境中,程序试图从 /opt/ros/kinetic/lib/python2./dist-packages/ 路径下导入cv2.so库文件。然而,该路径下的库文件是属于python2.7(普通环境)的,python3.5的cv2.so文件安装在虚拟环境对应的路径之下。

(RL_2018HW) gordon@gordon-:$ pip show opencv-python
Name: opencv-python
Version: 3.4.1.15
Summary: Wrapper package for OpenCV python bindings.
Home-page: https://github.com/skvark/opencv-python
Author: None
Author-email: None
License: MIT
Location: /home/gordon/python-virtual-environments/RL_2018HW/lib/python3./site-packages
Requires: numpy
Required-by: baselines

因此,虚拟环境中,程序得从 /home/gordon/python-virtual-environments/RL_2018HW/lib/python3./site-packages 路径下导入cv2.so文件。

解决方法

如何修改python程序读取库文件的路径呢?

首先介绍2种方法,用于查看python读取库文件路径(以python3.5的环境为例)

1. 读取环境变量$PYTHONPATH

(RL_2018HW) gordon@gordon-:~$ echo $PYTHONPATH
/home/gordon/python-virtual-environments/RL_2018HW/gym-gazebo:/home/gordon/ros_ws/devel/lib/python2./dist-packages:/opt/ros/kinetic/lib/python2./dist-packages:/opt/movidius/caffe/python

2. 在python控制台中查看

(RL_2018HW) gordon@gordon-:~$ python
Python 3.5. (default, Nov , ::)
[GCC 5.4. ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/gordon/python-virtual-environments/RL_2018HW/gym-gazebo', '/home/gordon/ros_ws/devel/lib/python2.7/dist-packages', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/opt/movidius/caffe/python', '/home/gordon/python-virtual-environments/RL_2018HW/lib/python35.zip', '/home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5', '/home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/plat-x86_64-linux-gnu', '/home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/site-packages', '/home/gordon/python-virtual-environments/RL_2018HW/baselines', '/home/gordon/python-virtual-environments/RL_2018HW/pybox2d/library']

可以发现,两种方式出现了路径 /opt/ros/kinetic/lib/python2./dist-packages ,且其优先级比路径 /home/gordon/python-virtual-environments/RL_2018HW/lib/python3./site-packages 高。

我们希望程序优先读取python3.5的库路径,对应有两种方式!

1. 修改环境变量$PYTHONPATH

  • 修改当前终端的环境变量
export PYTHONPATH=/home/gordon/python-virtual-environments/RL_2018HW/lib/python3./site-packages
  • 修改当前用户的环境变量

当使用命令激活ROS时,即运行 source /opt/ros/kinetic/setup.bash ,ROS会添加 /opt/ros/kinetic/lib/python2./dist-packages 到python路径。因此,需要在 .bashrc 文件中注释掉该命令行,并再次source读取环境变量。

2. 在python控制台中移除错误路径

(RL_2018HW) gordon@gordon-:~$ python
Python 3.5. (default, Nov , ::)
[GCC 5.4. ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line , in <module>
ImportError: /opt/ros/kinetic/lib/python2./dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>> import sys
>>> sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
>>> import cv2

解决python3.5无法导入cv2.so的问题的更多相关文章

  1. win10 +python3.6环境下安装opencv以及pycharm导入cv2有问题的解决办法

    一.安装opencv 借鉴的这篇博客已经写得很清楚了--------https://blog.csdn.net/u011321546/article/details/79499598       ,这 ...

  2. 解决 com.sun.*包导入错误

    解决 com.sun.*包导入错误 com.sun.image.codec.jpeg.*导入错误如何解决: com.sun.*是受限制访问的API,Eclipse 默认把受访问限制的API设成了ERR ...

  3. 解决jqplot与jquery-ui导入必要包时的冲突

    解决jqplot与jquery-ui导入必要包时的冲突 对于一个网页中,即要有jqplot的画图,又要有jquery-ui的风格显示! 但在导入必要的包时,出现了问题! 先导入jqplot的必要包: ...

  4. 解决Python3 pip list 红色DEPRECATION

    解决Python3 pip list 红色DEPRECATION 打开文件扩展名和隐藏的项目 找到ProgramData,在该目录下创建pip文件夹,在pip里面创建pip.ini  在pip.ini ...

  5. python基础===解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX(转载)

    本文转自:解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX 从网上抓了一些字 ...

  6. 解决python3缺少zlib的问题

    解决python3缺少zlib的问题 Table of Contents 1. 安装zlib 2. 重新编译安装python 3. 补充说明 在使用python3运行spark时,报缺少zlib的错误 ...

  7. 解决Python3下map函数的显示问题

    今天小编就为大家分享一篇解决Python3下map函数的显示问题,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.P ...

  8. 如何解决spring boot 项目导入依赖后代码报错问题

    如何解决spring boot 项目导入依赖后代码报错问题 2020-08-15  14:17:18 代码截图如图所示(由于本人问题已经解决,没来得及截图,所以在网上找了一张图片)

  9. Python3中无法导入ssl模块的解决办法

    这个问题,已经困扰我好几天了,本萌新刚开始接触python,想爬取几个网页试试,发现urllib无法识别https,百度后才知道要导入ssl模块,可是发现又报错了. 本人实在无法理解为什么会报错,因为 ...

随机推荐

  1. c3p0 操作

    E3p0连接池 c3p0-config.xml c3p0-config.xml(必须叫这个名字),然后必须放在工程目录的src下面 注意:c3p0里面可以配置多个连接信息,可以给每个配置起个名字,这样 ...

  2. Redux和react-redux的学习总结

    写在最前面:这段时间一直在看前端方面的东西,之前只是了解HTML,CSS,JS,jQuery,由于公司交代了前端的任务,所以后面又看了Bootstrap,React,Redux,react-redux ...

  3. Petrozavodsk Winter Camp, Andrew, 2014, Bipartite Bicolored Graphs

    由i个点和j个点组成的二分图个数为 $3^{ij}$,减去不联通的部分得到得到由i,j个点组成的联通二分图个数 $g_{i,j} = 3_{ij} - \sum_{k=1}^i \sum_{l=0}^ ...

  4. arrow function、function.apply

    An arrow function expression has a shorter syntax than a function expression and does not have its o ...

  5. 使用nginx实现负载均衡的配置

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  6. sin n次方 x 的降幂公式

    A(n) = ∫ sinⁿx dx= ∫ sinⁿ⁻¹xsinx dx= - ∫ sinⁿ⁻¹x d(cosx)= - sinⁿ⁻¹xcosx + ∫ cosx • d(sinⁿ⁻¹)= - sinⁿ ...

  7. 201621123075 week5 继承、多态、抽象类和接口

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.abstract.implements.comparable 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一 ...

  8. windows下《Go Web编程》之Go命令

    Go命令: go build 用于编译代码,默认会编译当前目录下的所以go文件.若只需编译某个文件,go build后加上文件名,如go build a.go. go build会忽略目录下以“_”或 ...

  9. Forth 内存布局

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  10. Spring boot 启动报错 Failed to auto-configure a DataSource

    1.Spring boot 启动报错 Failed to auto-configure a DataSource 参考资料https://blog.csdn.net/liuyinfei_java/ar ...