简体   繁体   English

如何在Mac OS中将openCv与Python3链接

[英]How to link openCv with Python3 in mac Os

I have two versions of python in my mac os, the first python2.7 which is the default that came with the system. 我的Mac OS中有两个版本的python,第一个python2.7是系统随附的默认版本。 Later I installed python3.7 that I use most of the time. 后来我安装了大部分时间使用的python3.7。

I have recently installed openCV using homebrew. 我最近使用自制软件安装了openCV。 When I'm using openCV with python2.7, it's working normally. 当我在python2.7中使用openCV时,它可以正常工作。

But the problem is when I try to use it with python3. 但是问题是当我尝试将其与python3一起使用时。 Importing cv2 in python3 gives error: ModuleNotFoundError: No module named 'cv2' 在python3中导入cv2会产生错误:ModuleNotFoundError:没有名为“ cv2”的模块

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current 
information.

>>> import cv2
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
>>> 

Is there anything I can do so that I can link the installed openCV with python3 ?? 有什么我可以做的,以便可以将已安装的openCV与python3链接?

Thanks for the help 谢谢您的帮助

Sorry I cannot give you specifics because your setup is not identical to mine, but I am sure we can get you sorted out. 抱歉,我无法提供具体信息,因为您的设置与我的设置不同,但是我相信我们可以帮助您解决问题。

Firstly, when you install packages, such as OpenCV , they tend to create a directory somewhere called lib which contains the C/C++ functions you can call from that package. 首先,当您安装诸如OpenCV之类的软件包时,它们倾向于在一个名为lib的目录中创建一个目录,其中包含可从该软件包调用的C / C ++函数。 Inside that directory, you normally find "shared object libraries" which traditionally end in "XXX.so" on macOS. 在该目录中,通常可以找到“共享对象库” ,该通常在macOS上以“ XXX.so”结尾。 More interestingly, they also contain a sub-directory called site-packages which contains the Python bindings (links). 更有趣的是,它们还包含一个名为site-packages的子目录,其中包含Python绑定(链接)。 So, on my system, which is likely different from yours, I can find all those site-packages directories with: 因此,在您的系统上(可能与您的系统不同),我可以使用以下命令找到所有这些site-packages目录:

find / -type d -name site-packages 2>/dev/null

Sample Output 样本输出

/usr/local/lib/python3.7/site-packages
/usr/local/lib/python2.7/site-packages
...
...
/usr/local/Cellar/tbb/2018_U5/lib/python2.7/site-packages
/usr/local/Cellar/vips/8.6.5/lib/python3.7/site-packages

Hopefully, you can see that /usr/local/lib/python3.7/site-packages is looking a very likely candidate for where all the Python v3.7 bindings for OpenCV should be. 希望您可以看到/usr/local/lib/python3.7/site-packages看起来很可能适合OpenCV的所有Python v3.7绑定。

Good, so now we know how to find the Python bindings, we need to tell Python that information. 好,所以现在我们知道了如何找到Python绑定,我们需要告诉Python该信息。 How? 怎么样? Well, not unreasonably, Python looks at an environment variable called PYTHONPATH to find its stuff. 嗯,并非没有道理,Python会查看一个名为PYTHONPATH的环境变量来查找其内容。 So, using our skill and judgement we need to marry up what we found in the first step with what we now know from the second step. 因此,利用我们的技能和判断力,我们需要将第一步中发现的内容与第二步中已知的内容结合起来。 So we do: 因此,我们这样做:

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages

And everything should work. 一切都应该工作。 All we need to do is put that in our login profile (probably $HOME/.profile ) and we will be ready to go every time we log in. 我们需要做的就是将其放入我们的登录配置文件(可能是$HOME/.profile )中,并且每次登录时我们都准备就绪。

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

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