Travis CI: "Unable to locate package python-opencv" Python 2.7

11,380

After:

sudo add-apt-repository python-opencv

You need

sudo apt-get update

So that the new repository information is correctly updated; before you can add packages from that repository.

Share:
11,380
Michael Currie
Author by

Michael Currie

BMath, University of Waterloo, 2007. Experience with SQL, Python, JavaScript, C++, VBA.

Updated on June 05, 2022

Comments

  • Michael Currie
    Michael Currie about 2 years

    On a normal non-virtualenv Ubuntu machine I can run:

    sudo apt-get install python-opencv
    

    And then from Python 2.7 I can run import cv2. Success!

    But when I try to do the very same thing in my .travis.yml file for automated testing, I get the error:

    E: Unable to locate package python-opencv

    How can I get apt-get to locate python-opencv in my Travis-CI build?

    I've tried the following; all were unsuccessful:

    1. From https://askubuntu.com/questions/339217/, I tried appending these lines to /etc/apt/sources.list:

      echo "deb http://de.archive.ubuntu.com/ubuntu precise main restricted universe" | sudo tee -a /etc/apt/sources.list
      echo "deb-src http://de.archive.ubuntu.com/ubuntu precise restricted main multiverse universe" | sudo tee -a /etc/apt/sources.list
      echo "deb http://de.archive.ubuntu.com/ubuntu precise-updates main restricted universe" | sudo tee -a /etc/apt/sources.list
      echo "deb-src http://de.archive.ubuntu.com/ubuntu precise-updates restricted main multiverse universe" | sudo tee -a /etc/apt/sources.list
      
    2. From here I tried adding these lines right before:

      sudo apt-get install python-software-properties
      sudo add-apt-repository python-opencv
      
    3. Following this, with updated method from here, I tried using this instead of 2.7:

    python: - "2.7_with_system_site_packages"

    (My full .travis.yml file is here.)

    Update

    Burhan Khalid's answer did get OpenCV installed, so the error went away. However, then when I tried find the package using import cv2 it still couldn't find it, because the Travis-CI build machine is wrapped in a virtualenv. So we can't access packages outside of our hermetically-sealed build environment.

    So I build from source. (References: here, here and here)

    Here's how to do it in the .travis.yml file:

    env:
      global:
        # Dependencies
        - DEPS_DIR="`readlink -f $TRAVIS_BUILD_DIR/..`"
        - OPENCV_BUILD_DIR=$DEPS_DIR/opencv/build
    

    And then, in the before_install section:

      - travis_retry git clone --depth 1 https://github.com/Itseez/opencv.git $DEPS_DIR/opencv
      - mkdir $OPENCV_BUILD_DIR && cd $OPENCV_BUILD_DIR
    
      - |
          if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then 
            cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..
          else
            cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON_EXECUTABLE=$(which python3) -DPYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..
          fi
      - make -j4
      - sudo make install
    
      - echo "/usr/local/lib" | sudo tee -a /etc/ld.so.conf.d/opencv.conf
      - sudo ldconfig
      - echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig" | sudo tee -a /etc/bash.bashrc
      - echo "export PKG_CONFIG_PATH" | sudo tee -a /etc/bash.bashrc
      - export PYTHONPATH=$OPENCV_BUILD_DIR/lib/python3.3/site-packages:$PYTHONPATH 
    
  • Michael Currie
    Michael Currie over 8 years
    This installs openCV on the machine, but it's no good because it's isolated from my Travis-CI virtualenv. See the update in my question.