Ubuntu16.04 安装了ROS Kinetic之后Python3不能import cv2

安装了ROS Kinetic之后Python3不能import Opencv

问题描述如下:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>> 


Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 

据了解在,python中是通过cv2.so调用opencv的库的:参考链接

Python调用opencv的原理是:opencv编译出共享库文件,python把这个共享库文件作为一个模块加载并使用。通俗点就是,编译opencv的时候开启python接口选项,编译好了会产生cv2.so(linux下)或者cv2.pyd(windows下)这个共享库文件,python代码中import这个cv2就可以用了。为了能正确import它,往往需要把cv2.so放在python找包能找到的路径下,或者修改PYTHONPATH环境变量让它包含cv2.so所在路径。

可以看出这个问题是由ROS添加/opt/ros/kinetic/lib/python2.7/dist-packages到python路径引起的。

当使用命令激活ROS时,实际上会发生这种情况source /opt/ros/kinetic/setup.bash。这行通常会添加到bashrc文件的末尾/home/username/.bashrc

解决方法是从bashrc文件中删除此行。

这样就可以正确使用python3 opencv包,你仍然可以运行source /opt/ros/kinetic/setup.bash使用ROS。但是,这确实意味着无法在同一环境中使用ROS和python3

就是说,在用python3运行opencv3 即import cv2的时候需要把.bashrc 或者 .zshrc中的有关ros的soure都注释掉,才可以正常运行

给Python3配置opencv

按网上搜索到的文章来讲,使用pip install opencv-python即可以给python3配置上opencv,但是在我这样安装了之后还是不行,于是选择了sudo pip install opencv-python但是出现了如下的错误:

The directory '/home/sun/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/sun/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): opencv-python in /home/sun/.local/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.11.1 in /home/sun/.local/lib/python3.5/site-packages (from opencv-python)
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

搜索之后发现解决方案如下:使用sudo -H pip3 install opencv-python安装即可

# 安装之后 进入/usr/local/lib/python3.5/dist-packages/ 会发现有一个cv2的文件夹 里边有.so文件
sun@MaxChanger  /usr/local/lib/python3.5/dist-packages/cv2  ls
cv2.cpython-35m-x86_64-linux-gnu.so  data  __init__.py  LICENSE-3RD-PARTY.txt  LICENSE.txt  __pycache__

pip install & sudo pip install & pip install –user的区别

$ pip install
# 安装目录  /home/sun/.local/lib/python3.5/site-packages
$ sudo pip install     
# 在python安装中全局安装包,即对所有用户安装。
# 安装目录 /usr/local/lib/python3.5/dist-packages
$ pip install --user   #安装到本地用户目录,即~/.local/lib/python - 只是你。 

查看python的安装路径

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/sun/WorkSpace/Ros_workspace/catkin_cartographer/install_isolated/lib/python2.7/dist-packages', '/home/sun/WorkSpace/Ros_workspace/catkin_slam/devel/lib/python2.7/dist-packages', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/sun/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
>>> 
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/sun/WorkSpace/Ros_workspace/catkin_cartographer/install_isolated/lib/python2.7/dist-packages', '/home/sun/WorkSpace/Ros_workspace/catkin_slam/devel/lib/python2.7/dist-packages', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/sun/.local/lib/python3.5/site-packages', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
>>> 

dist-packages 和 site-packages 的区别

这允许你让两个安装隔离开来。

  • dist-packages:系统自带的python

  • site-packages:自己安装的python

  • sudo apt-get install 安装的package存放在 /usr/lib/python2.7/dist-packages目录中

  • pip 或者 easy_install安装的package存放在/usr/local/lib/python2.7/dist-packages目录

Ubuntu下设置环境变量有三种方法

一种用于当前终端,一种用于当前用户,一种用于所有用户:

  • 用于当前终端:

在当前终端中输入:export PATH=$PATH:<你的要加入的路径>

不过上面的方法只适用于当前终端,一旦当前终端关闭或在另一个终端中,则无效。

export NDK_ROOT=/home/jiang/soft/Android-ndk-r8e#只能在当前终端使用。

  • 用于当前用户:

在用户主目录下有一个 .bashrc 隐藏文件,可以在此文件中加入 PATH 的设置如下:$ gedit ~/.bashrc

加入:export PATH=<你的要加入的路径>:$PATH

如果要加入多个路径,只要:export PATH=<你要加入的路径1>:<你要加入的路径2>: ...... :$PATH

当中每个路径要以冒号分隔。

这样每次登录都会生效

添加PYTHONPATH的方法也是这样,在.bashrc中添加

export PYTHONPATH=/home/zhao/setup/caffe-master/python:/home/zhao/setup/mypy:$PYTHONPATH

保存后在终端输入 $ source ~/.bashrc使环境变量立即生效

  • 用于所有用户:

$ sudo gedit /etc/profile

加入:export PATH=<你要加入的路径>:$PATH就可以了。

终端输入:echo $PATH可以查看环境变量

注意,修改环境变量后,除了第一种方法立即生效外,第二第三种方法要立即生效,可以source ~/.bashrc或者注销再次登录后就可以了!

[参考链接] https://stackoverflow.com/questions/43019951/after-install-ros-kinetic-cannot-import-opencv
“安装了ROS Kinetic之后Python3不能import Opencv”

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值