Build Python bindings separately

asked 2016-08-31 20:01:59 -0500

bjm900 gravatar image


Is it possible to build the Python bindings for OpenCV separately to the main build - something like a standard distutils

On our supercomputer cluster we maintain quite a few versions of Python. As with all packages, each new version gets built and installed into it's own directory. Each of these versions needs their own Python bindings, linked against that particular version of Python, so what we usually do is install the main C/C++ libraries as usual and then build Python packages for each version as needed, linking against the already-installed C/C++ libraries. However, I can't work out how to separate the Python binding build for OpenCV from the main C/C++ build.

Our base application tree looks like this at the moment:

  • /apps/opencv/x.x.x (OpenCV x.x.x. installation - already have)
  • /apps/python/y.y.y (Python y.y.y installation - already have)

and we want to build and install the bindings at

  • /apps/opencv-python/x.x.x-y.y.y

The we can easily allow users to mix and match Python and OpenCV versions using the standard environment modules (e.g. doing module load opencv/1.2.3, module load python/4.5.6, module load opencv-python would add /apps/opencv-python/x.x.x-y.y.y to PYTHONPATH.

Could you let me know how we can do this?

Thanks, Ben

edit retag flag offensive close merge delete


1st thing to check: are you building with BUILD_SHARED_LIBS=OFF ?

(so your no more depends on further opencv so's at runtime)

then, imho, you should be able to move around your as you whish.

berak gravatar imageberak ( 2016-08-31 23:20:06 -0500 )edit

That would still involve rebuilding OpenCV for each version of Python, yes? I wanted to just build the Python bindings (i.e. and link them against the .so files in the main OpenCV install. E.g. for standalone packages I'd just make sure the OpenCV libraries are on-path and add -lopencv or similar to the link line.

bjm900 gravatar imagebjm900 ( 2016-08-31 23:30:28 -0500 )edit

" I wanted to just build the Python bindings (i.e. and link them against the .so files in the main OpenCV install." -- i do not think, this is possible. (i.e. building the python wrappers involves re-building some dependant modules like core, imgproc, etc.)

berak gravatar imageberak ( 2016-08-31 23:43:33 -0500 )edit

Hmm, okay - so some of the OpenCV modules depend on That seems unfortunate.

bjm900 gravatar imagebjm900 ( 2016-08-31 23:47:08 -0500 )edit

hmm, more like: depends on libpython.a (other opencv modules don't)

berak gravatar imageberak ( 2016-08-31 23:52:10 -0500 )edit

But do some of the bits of the Python bindings live in the main OpenCV libraries, not just cv2? Otherwise, why does building the Python bindings need to re-build OpenCV modules?

bjm900 gravatar imagebjm900 ( 2016-08-31 23:57:10 -0500 )edit

let me put it another way:

the python/java wrappers are script-generated from opencv's src code. then, the resulting stub code has to get linked against 1. python, 2. opencv libs (either static or dynamic)

berak gravatar imageberak ( 2016-09-01 00:00:47 -0500 )edit

Ahh, okay. That makes sense, thanks for the link.

bjm900 gravatar imagebjm900 ( 2016-09-01 01:30:58 -0500 )edit

I don't know if this helps, but I published recently Python wheels for opencv-python 3.1.0 to PyPI ( For example with virtualenvs they can be used to run OpenCV with multiple different Python versions.

skvark gravatar imageskvark ( 2016-09-05 15:25:00 -0500 )edit