Installing OpenCV for Python on Ubuntu, getting ImportError: No module named cv2.cv

后端 未结 20 849
遥遥无期
遥遥无期 2020-11-28 23:48

I have an Ubuntu 14.04 system, on which I want to install OpenCV and use it with Python 2.x.

I installed OpenCV using the instructions here: https://help.ubuntu.com/

相关标签:
20条回答
  • 2020-11-29 00:44

    try using sudo apt install python3-opencv

    it will install the latest package of open cv.

    Or you could try reinstalling the opencv package. It might have got corrupted during installation.

    0 讨论(0)
  • 2020-11-29 00:46

    Verify if cv2.so did compile, should be placed in: /usr/local/lib/python2.7/site-packages Then export that path like this

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

    Same as in the answer here

    0 讨论(0)
  • 2020-11-29 00:51

    Use pip:

    https://pypi.python.org/pypi/pip

    $ pip install SomePackage
      [...]
      Successfully installed SomePackage
    

    And when you add a path to PYTHONPATH with sys, PYTHONPATH it's always restarted to default values when you close your Python shell. Check this thread:

    Permanently add a directory to PYTHONPATH

    First add openCV to your path (Quick guide):

    https://help.ubuntu.com/community/OpenCV

    after that, install the non-python packages pyopencv depends on:

    sudo apt-get build-dep python-opencv
    

    finally, use pip:

    pip install pyopencv
    

    Also, you can check this tutorial to install openCV in ubuntu 14.04 LTS

    http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

    0 讨论(0)
  • 2020-11-29 00:51

    This seemed to work for me on Max OSX: https://anaconda.org/menpo/opencv3

    conda install -c menpo opencv3=3.1.0
    

    I confirmed that you can import cv2 in python using python2.7 and python3

    0 讨论(0)
  • 2020-11-29 00:51

    For me, this problem was due to the fact that I had not appropriately sym-linked the cv2.so file in the~/.virtualenvs/cv/lib/python3.5/site-packages folder (the name of your virualenv may not be "cv", your version of python may not be 3.5--adjust accordingly).

    If you go to the ~/.virtualenvs/cv/lib/python3.5/site-packages folder and ls, the cv2.so file should appear in light blue (Ubuntu 16.04) showing that it is linked. You can check the link location by typing: readlink cv2.so

    If cv2.so appears in red (as mine did), rm the file and type: (for my install of python 3.5)

    ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
    

    OR (if you have python 3.6)

    ln -s /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
    

    If you are working in python 2.6 or python 2.7, you instead type:

    ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
    

    If the cv2.so or cv2.cpython-36m-x86_64-linux-gnu.so files do not exist in your /usr/local/lib/python***/dist-packages location, check to see if they're in a /usr/local/lib/python***/sites-packages folder. If so, adjust the path accordingly. If not, something has gone wrong with your opencv installation.

    This answer was inspired by information here: https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

    0 讨论(0)
  • 2020-11-29 00:52

    I tried all the other options here, but I could not get import cv2 working with Anaconda on Ubuntu. This is the only thing that helped:

    pip install opencv-python

    0 讨论(0)
提交回复
热议问题