How install opencv to a venv python3 dist-packages?

With …

cd opencv/build
cmake -D CMAKE_BUILD_TYPE=RELEASE  \
     -D CMAKE_INSTALL_PREFIX=/usr/local \
     -D INSTALL_PYTHON_EXAMPLES=ON \
     -D INSTALL_C_EXAMPLES=OFF \
     -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
     -D OPENCV_GENERATE_PKGCONFIG=ON \
     -D BUILD_EXAMPLES=ON \
     -D PYTHON3_EXECUTABLE=/usr/bin/python3 \
     -D PYTHON3_INCLUDE_DIR=/usr/include/python3.7 \
     -D PYTHON3_INCLUDE_PATH=/usr/include/python3.7 \
     -D PYTHON3_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.7 \
     -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.7 \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.7/dist-packages/numpy/core/include \
     -D OPENCV_PYTHON_INSTALL_PATH_SETUPVARS=lib/python3.7/dist-packages \
     ..

… I’m told that the install path with still be python 3.6:

-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/sir/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /home/sir/venv/p37_default/bin/python3 (ver 3.6.9)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.7 (ver 3.6.9)
--     numpy:                       /usr/local/lib/python3.7/dist-packages/numpy/core/include (ver 1.16.1)
>>> @!!! Eh... Wut?!  3.6 Should be 3.7
--     install path:                lib/python3.6/dist-packages/cv2/python-3.6
-- 
--   Python (for build):            /home/sir/venv/p37_default/bin/python
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/java-8-oracle/include /usr/lib/jvm/java-8-oracle/include/linux /usr/lib/jvm/java-8-oracle/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sir/opencv/build

How can I target the install at a venv dist-packages directory?

Many references suggest simply copying the .so into the dist_packages/cv2 directory.

cp ../build/lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so ~/venv/p37_default/lib/python3.7/site-packages/cv2/

However this leaves me with:

  File "/home/sir/Dropbox/software/vision_noodle/util.py", line 13, in <module>
    import cv2
  File "/home/sir/venv/p37_default/lib/python3.7/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5: symbol _ZNK11QLocaleData13validateCharsERK7QStringNS_10NumberModeEP10QByteArrayi6QFlagsIN7QLocale12NumberOptionEE version Qt_5 not defined in file libQt5Core.so.5 with link time reference

sounds broken, I agree.

are there any remains of a previous build, e.g. a cmakecache.txt? are there any environment variables that point to a python 3.6?

Environment vars - no, but there did seem to be an issue with state.
Blowing away the build directory produced the right options except the summary showed:

   --   Python (for build):            /home/sir/venv/p37_default/bin/python

as the system python 2.7. Overriding PYTHON_DEFAULT_EXECUTABLE (which I found via grep 2.7 CMakeVars.txt
seemed to fix the summary:

cd ~/opencv
rm -rf ~/opencv/build
mkdir ~/opencv/build
cd ~/opencv/build
cmake -D CMAKE_BUILD_TYPE=RELEASE  \
     -D CMAKE_INSTALL_PREFIX=/usr/local \
     -D INSTALL_PYTHON_EXAMPLES=ON \
     -D INSTALL_C_EXAMPLES=OFF \
     -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
     -D OPENCV_GENERATE_PKGCONFIG=ON \
     -D BUILD_NEW_PYTHON_SUPPORT=ON \
     -D BUILD_EXAMPLES=ON \
     -D PYTHON3_INCLUDE_DIR=/usr/include/python3.7 \
     -D PYTHON3_INCLUDE_PATH=/usr/include/python3.7 \
     -D PYTHON3_EXECUTABLE=`which python` \
     -D PYTHON_DEFAULT_EXECUTABLE=`which python` \
     ..
make -j8
sudo make install >& log.txt

Installs to the site python:

-- Installing: /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so

… which I’m a little confused about since python-3.7 doesn’t appear in build/CMakeVars.txt.

1 Like