解決python3.5沒法導入cv2.so的問題

問題描述python

在python3.5環境中導入cv2報錯,在python2.7中正常。注:命令行的前綴RL_2018HW是python3.5的環境linux

(RL_2018HW) gordon@gordon-5577:$ python
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

gordon@gordon-5577:$ 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 cv2

從報錯能夠看出,在python3.5虛擬環境中,程序試圖從 /opt/ros/kinetic/lib/python2.7/dist-packages/ 路徑下導入cv2.so庫文件。然而,該路徑下的庫文件是屬於python2.7(普通環境)的,python3.5的cv2.so文件安裝在虛擬環境對應的路徑之下。git

(RL_2018HW) gordon@gordon-5577:$ 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.5/site-packages
Requires: numpy
Required-by: baselines

所以,虛擬環境中,程序得從 /home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/site-packages 路徑下導入cv2.so文件。github

 

解決方法bash

如何修改python程序讀取庫文件的路徑呢?app

首先介紹2種方法,用於查看python讀取庫文件路徑(以python3.5的環境爲例)python2.7

1. 讀取環境變量$PYTHONPATHui

(RL_2018HW) gordon@gordon-5577:~$ echo $PYTHONPATH 
/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

2. 在python控制檯中查看spa

(RL_2018HW) gordon@gordon-5577:~$ python
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/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.7/dist-packages ,且其優先級比路徑 /home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/site-packages 高。命令行

咱們但願程序優先讀取python3.5的庫路徑,對應有兩種方式!

1. 修改環境變量$PYTHONPATH

  • 修改當前終端的環境變量
export PYTHONPATH=/home/gordon/python-virtual-environments/RL_2018HW/lib/python3.5/site-packages
  • 修改當前用戶的環境變量

當使用命令激活ROS時,即運行 source /opt/ros/kinetic/setup.bash ,ROS會添加 /opt/ros/kinetic/lib/python2.7/dist-packages 到python路徑。所以,須要在 .bashrc 文件中註釋掉該命令行,並再次source讀取環境變量。

2. 在python控制檯中移除錯誤路徑

(RL_2018HW) gordon@gordon-5577:~$ python
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
>>> import sys
>>> sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
>>> import cv2
相關文章
相關標籤/搜索