简体   繁体   English

在python3上导入cv2

[英]import cv2 on python3

I have a problem trying to import cv2 in python3 when I run it from my home folder... 从主文件夹运行cv2时,尝试在python3中导入cv2时遇到问题...

luis@luis-Ubuntu:~$ python3
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
>>> 

but when I run python3 inside /usr/lib/python3/dist-packages or /usr/local/lib/python3.5/dist-packages path it works fine... 但是当我在/ usr / lib / python3 / dist-packages或/usr/local/lib/python3.5/dist-packages路径中运行python3时,它工作正常...

luis@luis-Ubuntu:~$ cd /usr/lib/python3/dist-packages
luis@luis-Ubuntu:/usr/lib/python3/dist-packages$ python3
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
>>> 

I know this is related to include a path on the sys library, I added the export PYTHONPATH on my ~/.bashrc but didn't solve the problem... 我知道这与在sys库中包含路径有关,我在〜/ .bashrc上添加了导出PYTHONPATH,但没有解决问题...

export PYTHONPATH="/usr/local/lib/python3.5/dist-package‌​s:$PYTHONPATH"

I also found that if I insert the path before importing cv2 it works but I need to do this on all scripts or every time I run python3 from a terminal... 我还发现,如果我在导入cv2之前插入路径,则可以工作,但是我需要在所有脚本上执行此操作,或者每次从终端运行python3时都需要执行此操作...

luis@luis-Ubuntu:~$ python3
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.insert(0,'/usr/lib/python3/dist-packages')
>>> import cv2
>>> 

but I want to fix this permanently, does anyone know how to solve this... btw... runs fine on python2... 但我想永久修复此问题,有人知道如何解决此问题吗... btw ...在python2上运行良好...

From the print(sys.path) that you have provided, 从您提供的print(sys.path)

...'/opt/ros/kinetic/lib/python2.7/dist-packages'...

I think the problem lies in this item. 我认为问题出在这个项目上。 Although the python3 path also resides in sys.path , the python2.7 path precedes it. 尽管python3路径也位于sys.path ,但python2.7路径位于它之前。 Python will catch the one in python2.7 first. Python将首先在python2.7捕获一个。 When you are running in directly inside /python3/dist-packages , the current directory is placed first, and that precedes python2.7 . 当直接在/python3/dist-packages内部运行时,当前目录位于python2.7之前,放在最前面。

I think there are two ways: 我认为有两种方法:

  1. Remove python2.7/dist-packages from your PYTHONPATH 从您的PYTHONPATH移除python2.7/dist-packages PYTHONPATH
  2. Call sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') before you import cv2 . 在导入cv2之前,请调用sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') See this question . 看到这个问题

The first approach should be the "proper" way but I am not sure how it is deal with if you want to use both python2/3 at the same time. 第一种方法应该是“正确”的方法,但是如果您想同时使用两个python2 / 3,我不确定如何处理。 You might be interested in this post . 您可能对此职位感兴趣。

The root of this problem are the ROS commands like source/opt/ros/kinetic/setup.bash in the bashrc file /home/username/.bashrc , which force changes in the Python path. 这个问题的根源是ROS命令,例如bashrc文件/home/username/.bashrc中的source/opt/ros/kinetic/setup.bash ,它会强制更改Python路径。 Even if you are not using ROS, the commands are still executed and thus you are directed to find cv2 in /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so in the ROS folder, which is not compatible with Python 3. More discussion on the same issue can be seen at: After install ROS Kinetic, cannot import OpenCV . 即使您没有使用ROS,命令仍然会执行,因此您将被引导在ROS文件夹中的/opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so中找到cv2。与Python 3兼容。有关同一问题的更多讨论,请参见: 安装ROS Kinetic后,无法导入OpenCV Here I propose another solution which is not mentioned by any answer at that post. 在这里,我提出了另一种解决方案,该职位没有任何答案。

The idea is to run source/opt/ros/kinetic/setup.bash (and any other bash related to ROS) only when using ROS. 这个想法是仅在使用ROS时运行source/opt/ros/kinetic/setup.bash (以及与ROS相关的任何其他bash)。 This way you do not have to modify .bashrc file (or editing the PYTHONPATH like to what you do) every time. 这样,您不必每次都修改.bashrc文件(或像执行操作一样编辑PYTHONPATH)。

First, remove ROS-related commands like source/opt/ros/kinetic/setup.bash from the basrch file and make sure that you can import cv2 in Python 3.x with no error now. 首先,从basrch文件中删除与ROS相关的命令,例如source/opt/ros/kinetic/setup.bash ,并确保可以import cv2在Python 3.x中import cv2

Then, create a environment and install all the ROS related packages here. 然后,创建环境并在此处安装所有与ROS相关的软件包。 By doing so, we will have to activate this ros_environment when running ROS. 这样,我们将必须在运行ROS时激活此ros_environment。 For creating environment, see https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands 有关创建环境的信息,请参阅https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands

Next, activate your newly-created environment and follow https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux to create files in the suggested path. 接下来,激活您新创建的环境,并按照https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux在建议的路径中创建文件。 Namely,

cd $CONDA_PREFIX cd $ CONDA_PREFIX
mkdir -p ./etc/conda/activate.d mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh 触摸./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh 触摸./etc/conda/deactivate.d/env_vars.sh

Edit the ./etc/conda/activate.d/env_vars.sh as follows 如下编辑./etc/conda/activate.d/env_vars.sh

    source/opt/ros/kinetic/setup.bash

Also add in any other ROS-related bash in the file. 还要在文件中添加其他与ROS相关的bash。

As for ./etc/conda/deactivate.d/env_vars.sh , what I do is exporting the PYTHONPATH back to Python 3. For example, it could be 至于./etc/conda/deactivate.d/env_vars.sh ,我要做的是将PYTHONPATH导出回./etc/conda/deactivate.d/env_vars.sh 。例如,它可能是

    export PYTHONPATH="/usr/lib/python3/dist-packages"

Note that this is not really deactivating the source/opt/ros/kinetic/setup.bash command. 请注意,这实际上并没有取消激活source/opt/ros/kinetic/setup.bash命令。 I just found doing this way would direct my PYTHONPATH back to the default python 3, and the correct cv path can be found even after activation and deactivation of the ROS environment . 我只是发现这样做可以将我的PYTHONPATH重定向回默认的python 3,并且即使在激活和停用ROS环境后也可以找到正确的cv路径。

You can add the following two lines before you import cv2 in your python code. 在将cv2导入python代码之前,可以添加以下两行。 It works for me without changing any source file: 它对我有效,无需更改任何源文件:

import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')

Then import cv2: 然后导入cv2:

import cv2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM