How to give opencv dependency to python package

https://github.com/NixOS/nixpkgs/pull/153255

I want a python package to take a opencv4 dependency, but if I put the dependency in propagatedBuildInputs like https://github.com/NixOS/nixpkgs/blob/6aec41eebecce8a296442a71dcfe8727e812914d/pkgs/development/python-modules/cmapy/default.nix#L10 it still complains about missing python-opencv.

How do I say to use opencv for its python-opencv?

It may be that you are using opencv when you need the Python package instead.

Maybe try changing to:

  fake-background-webcam = callPackage ../applications/video/fake-background-webcam {
    opencv = python3Packages.opencv4;
  };

I am already building it by nix build .#python3Packages.fake-background-webcam so it should take the opencv from the python package set
https://github.com/NixOS/nixpkgs/blob/6aec41eebecce8a296442a71dcfe8727e812914d/pkgs/top-level/python-packages.nix#L5557
And yet it does not seem to recognize it.

1 Like

opencv4 is incorrectly packaged:

$ tree $(nix-build '<nixpkgs>' -A python3Packages.opencv4)/lib/python3.9/
/nix/store/dfylw3ppidn6gpbxrkjhmpsazk7cdzdj-opencv-4.5.4/lib/python3.9/
└── site-packages
    └── cv2.cpython-39-x86_64-linux-gnu.so

There should be some distribution information

$ tree -L 3 $(nix-build '<nixpkgs>' -A python3Packages.requests)/lib/python3.9/
/nix/store/pa8f3ajnj32hp13is72yw8nfcd94bg1w-python3.9-requests-2.26.0/lib/python3.9/
└── site-packages
    β”œβ”€β”€ requests
     ....
    └── requests-2.26.0.dist-info
        β”œβ”€β”€ direct_url.json
        β”œβ”€β”€ INSTALLER
        β”œβ”€β”€ LICENSE
        β”œβ”€β”€ METADATA
        β”œβ”€β”€ RECORD
        β”œβ”€β”€ REQUESTED
        β”œβ”€β”€ top_level.txt
        └── WHEEL

That’s my suspicion, can you help me with those files?

I’m creating a PR to fix it for nixpkgs

Perfect please cc me

This was more of a pain than I thought it would be. But here’s the PR: python3Packages.opencv4: fix installation metadata by jonringer Β· Pull Request #153373 Β· NixOS/nixpkgs Β· GitHub

Couldn’t get it to actually β€œwork”

$ nix-shell -E 'with import ./. { }; mkShell { name = "test"; nativeBuildInputs = [ (python3.withPackages (ps: [ps.opencv4])) ]; }'

[nix-shell:~/projects/nixpkgs]$ python
Python 3.9.9 (main, Nov 15 2021, 18:05:17)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/b60icklg1rxdjddk8aysg14zyxnb6za0-python3-3.9.9-env/lib/python3.9/site-packages/cv2/__init__.py", line 180, in <module>
    bootstrap()
  File "/nix/store/b60icklg1rxdjddk8aysg14zyxnb6za0-python3-3.9.9-env/lib/python3.9/site-packages/cv2/__init__.py", line 110, in bootstrap
    load_first_config(['config.py'], True)
  File "/nix/store/b60icklg1rxdjddk8aysg14zyxnb6za0-python3-3.9.9-env/lib/python3.9/site-packages/cv2/__init__.py", line 108, in load_first_config
    raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

But this appears to be intended behavior, as pyinstaller is required ImportError: OpenCV loader: missing configuration file: ['config.py']. Β· Issue #569 Β· opencv/opencv-python Β· GitHub

Hmm, deleting all of the cv2 python files seems to make it work just fine as a module.

+    # the cv2/__init__.py just tries to check provide "nice user feedback" if the installation is bad
+    # however, this also causes infinite recursion when used by other packages
+    rm -r $out/${pythonPackages.python.sitePackages}/cv2
[11:20:57] jon@nixos ~/projects/nixpkgs (fix-opencv4)
$ nix-shell -E 'with import ./. { }; mkShell { name = "test"; nativeBuildInputs = [ (python3.withPackages (ps: [ps.opencv4])) ]; }'

[nix-shell:~/projects/nixpkgs]$ python
Python 3.9.9 (main, Nov 15 2021, 18:05:17)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

Apparently the module requires opencv-python,although it is used in the exact same way

interesting, I think that’s what the directory is called. But that’s now how they install it.

I’ve already tried symlinking opencv-ver.dist-info toopencv-python-ver.dist-inf but failed
https://github.com/NixOS/nixpkgs/blob/508e01344bdd2ac5cb197027070704b1d515ff16/pkgs/development/python-modules/opencv-python/default.nix