How do I access the OpenCV Bython bindings?

I’ve tried nix-shell -p opencv4 python3, then opening a Python shell and trying to import cv or cv2, but to no avail.

see https://github.com/NixOS/nixpkgs/issues/64363

I’m not sure I understand. So the bindings inside OpenCV itself aren’t Python 3.7 compatible. But https://pypi.org/project/opencv-python/ seems to be. That, however, isn’t included in python3Packages.

Edit: Got it, nix-shell -p python3Packages.opencv4 seems to work.

@OmnipotentEntity was referring a python module not packaged in NixOS - the one that comes from pypi. A proof in nixpkgs can be found by git grep opencv-python:

pkgs/development/python-modules/baselines/default.nix:    # in nixos anyways. Since opencv-python is not currently packaged, we
pkgs/development/python-modules/baselines/default.nix:    sed -ie '/opencv-python/d' setup.py
pkgs/development/python-modules/imgaug/default.nix:    broken = true; # opencv-python bindings aren't available yet, and look non-trivial

However, this command doesn’t fail:

$ nix-shell --packages python3Packages.opencv4 --run 'python -c "import cv2"'

What’s so confusing is that python3Packages.opencv4 has nothing to do with the pypi package opencv-python, these are too different packages and the later isn’t packaged yet. In a nix-shell with python3Packages.opencv4 you can see by looking at the values of $PYTHONPATH that it includes:

/nix/store/yzgqqs0197q7g1z3xcjw364m97118crp-opencv-4.1.0/lib/python3.7/site-packages

And inside this folder you can find a file:

cv2.cpython-37m-x86_64-linux-gnu.so

This file can be imported to Python directly through import cv2 and it comes out of the opencv4 package. See:

And:

1 Like