问题描述如下:
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 "" , line 1, in
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
都注释掉,才可以正常运行
按网上搜索到的文章来讲,使用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
# 安装目录 /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 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:系统自带的python
site-packages:自己安装的python
sudo apt-get install 安装的package存放在 /usr/lib/python2.7/dist-packages目录中
一种用于当前终端,一种用于当前用户,一种用于所有用户:
在当前终端中输入: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”