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.

1 Like


I’m not sure I understand. So the bindings inside OpenCV itself aren’t Python 3.7 compatible. But opencv-python · PyPI 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'
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:


And inside this folder you can find a file:

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


1 Like