title: Travis CI: "Unable to locate package python-opencv" Python 2.7
body: <p>On a normal non-virtualenv Ubuntu machine I can run:</p>
  
  <pre><code>sudo apt-get install python-opencv
  </code></pre>
  
  <p>And then from Python 2.7 I can run <code>import cv2</code>.  Success!</p>
  
  <p>But when I try to do the very same thing in my <code>.travis.yml</code> file for automated testing, I get the error: </p>
  
  <blockquote>
    <p>E: Unable to locate package python-opencv</p>
  </blockquote>
  
  <p><strong>How can I get apt-get to locate <code>python-opencv</code> in my Travis-CI build?</strong></p>
  
  <p>I've tried the following; all were unsuccessful:</p>
  
  <ol>
  <li><p>From <a href="https://askubuntu.com/questions/339217/">https://askubuntu.com/questions/339217/</a>, I tried appending these lines to <code>/etc/apt/sources.list</code>:</p>
  
  <pre><code>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
  </code></pre></li>
  <li><p>From <a href="https://web.archive.org/web/20130620145414/http://www.neuroforge.co.uk/index.php/getting-started-with-python-a-opencv" rel="nofollow noreferrer">here</a> I tried adding these lines right before:</p>
  
  <pre><code>sudo apt-get install python-software-properties
  sudo add-apt-repository python-opencv
  </code></pre></li>
  <li><p>Following <a href="http://danielnouri.org/notes/2012/11/23/use-apt-get-to-install-python-dependencies-for-travis-ci/" rel="nofollow noreferrer">this</a>, with updated method from <a href="https://github.com/travis-ci/travis-ci/issues/2219" rel="nofollow noreferrer">here</a>, I tried using this instead of <code>2.7</code>:</p></li>
  </ol>
  
  <p><code>python:
        - "2.7_with_system_site_packages"
  </code></p>
  
  <p>(My full <a href="https://github.com/openworm/movement_validation/blob/a1c6145790a7f5a431128c2d9a26070111fd3168/.travis.yml" rel="nofollow noreferrer"><code>.travis.yml</code> file is here</a>.)</p>
  
  <p><strong>Update</strong></p>
  
  <p>Burhan Khalid's answer did get OpenCV installed, so the error went away.  However, then when I tried find the package using <code>import cv2</code> 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.</p>
  
  <p>So I build from source. (References: <a href="https://github.com/kevin-brown/blog.kevin-brown.com/blob/2d5296a8c1ac76dc5cbd2b98d765749c043fc521/_posts/2014-09-27-building-and-installing-opencv-3.markdown" rel="nofollow noreferrer">here</a>, <a href="https://github.com/kashefy/elm/blob/76dbd0214dbab91a7de3bdd94767ef08aa67d0f4/.travis.yml" rel="nofollow noreferrer">here</a> and <a href="https://gist.github.com/itguy51/4239282" rel="nofollow noreferrer">here</a>)</p>
  
  <p>Here's how to do it in the <code>.travis.yml</code> file:</p>
  
  <pre><code>env:
    global:
      # Dependencies
      - DEPS_DIR="`readlink -f $TRAVIS_BUILD_DIR/..`"
      - OPENCV_BUILD_DIR=$DEPS_DIR/opencv/build
  </code></pre>
  
  <p>And then, in the <code>before_install</code> section:</p>
  
  <pre><code>  - travis_retry git clone --depth 1 https://github.com/Itseez/opencv.git $DEPS_DIR/opencv
    - mkdir $OPENCV_BUILD_DIR &amp;&amp; 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 
  </code></pre>