Skip to content
Advertisement

How to tell pip that a package(opencv) has been compiled from source

Because of some specific requirements I needed to compile a package (opencv with cuda support) from source.

After successfull compilation my python-environment is able to import opencv without a problem:

$ python
Python 3.7.7 (default, Mar 10 2020, 15:16:38) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2
>>> cv2.__version__
'4.3.0'
>>>

But if I try pip list opencv-python is not part of it:

Package              Version
-------------------- --------
absl-py              0.9.0
astor                0.8.1
dlib                 19.20.99
gast                 0.3.3
google-pasta         0.2.0
grpcio               1.30.0
h5py                 2.10.0
importlib-metadata   1.6.1
Keras-Applications   1.0.8
Keras-Preprocessing  1.1.2
Markdown             3.2.2
numpy                1.19.0
pip                  20.1.1
protobuf             3.12.2
setuptools           47.3.1
six                  1.15.0
tensorboard          1.14.0
tensorflow-estimator 1.14.0
tensorflow-gpu       1.14.0
termcolor            1.1.0
Werkzeug             1.0.1
wheel                0.34.2
wrapt                1.12.1
zipp                 3.1.0

The problem is that afterwards I need to install more packages via pip install -r requirements.txt and some of the packages listed in requirements.txt have opencv as a dependency. As pip is not aware of the opencv installation it now installes a different opencv version. Having two different versions installed along side each other does not sound like a clever solution for me… I could uninstall the pip install opencv later but that does not seem to be a good solution either…

So how can I make pip aware of the other opencv installation before running the pip install?

Advertisement

Answer

After you compile opencv, you can install the package with pip or python setup.py install. I would recommend building a Python wheel for opencv+cuda, and then installing that wheel. Having a wheel will make installing easier if you ever need to reinstall or make a new environment.

The general steps are:

  1. Compile opencv
  2. Change into the opencv python directory (with setup.py) and run python setup.py bdist_wheel
  3. Run auditwheel repair my-python-wheel-1.5.2-cp35-cp35m-linux_x86_64.whl (change the wheel filename) (from https://stackoverflow.com/a/42106034/5666087)

You can get auditwheel with pip install auditwheel.

Another useful reference is the build documentation for the opencv-python wheels, available at https://github.com/skvark/opencv-python#build-process. You can modify those steps to your build.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement