简体   繁体   中英

How to use OpenCV with python in OSX?

I wondering how can I import opencv libraries in python. I am using sublime3 but a noob to it. What I have tried is import cv in OSX terminal but the python quit unexpectedly with an error Segmentation fault: 11

My $PATHONPATH=/usr/local/Cellar/opencv/2.4.6.1/lib/python2.7/site-packages since I installed it with homebrew, so did opencv

The problem has been solved since I imported the library in the system default python version carried by the OSX but my OpenCV was installed by an external python version. To change the default python version for your system, simply exports it to the $PATH

$PATH = /usr/local/Cellar/python/2.7.6/bin/

It is the path I installed with homebrew. To check your default python version, you can type

python -c 'import sys;print(sys.version)'

in your terminal;

Besides, to use OpenCV in Sublime, builds a build-system with

{
    "path": "/usr/local/Cellar/python/2.7.6/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin",
    "env": 
    {
        "PYTHONPATH": "/usr/local/lib/python2.7:/usr/local/Cellar/python/2.7.6/bin"
    },
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Also, change the path in "path" and "env" of your own. Notice that the configuration data in build-system should be in JSON format like "env" does.

Wish this answer could be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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