Skip to content

Add CMake option to install pip metadata when Python bindings are installed just by CMake #21611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

Conversation

traversaro
Copy link

@traversaro traversaro commented Feb 14, 2022

When OpenCV's Python bindings are installed only via CMake by enabling the appropriate CMake option, the opencv-python package is not listed by pip list .

This is not problematic until a user tries to install a Python library that depends on opencv-python via pip install . . In that case, the Python opencv-python package already available in the system is ignored, and the PyPI version of opencv-python is installed itself.

This PR adds the CMake logic to generate the necessary METADATA and INSTALLER Python files so that even when just installed via cmake, the opencv-python python package can be listed by pip list .

This logic is disabled by default, but can be enabled by the option OPENCV_PYTHON_PIP_METADATA_INSTALL, to avoid conflicts with existing installation script of Python bindings in https://github.com/opencv/opencv-python that generate these metadata files via setup.py.

This PR originated from conda-forge/opencv-feedstock#299 and has been validated in conda-forge/opencv-feedstock#300, but it can be useful for all downstream packagers of OpenCV's python bindings. For example, Debian's python3-opencv is affected by the same problem.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@traversaro
Copy link
Author

For what regard documentation, this is a packager-oriented option, so I do not know where is the best place where to document this.

endif()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/METADATA "")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Metadata-Version: 2.1${NEW_LINE}")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Name: opencv-python${NEW_LINE}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opencv-python

It is just a name of the package from dedicated repository: https://github.com/opencv/opencv-python
It is not an entity of this upstream repository. Perhaps we should not have it here.

Moreover there are several conflicting subpackages from "opencv-python", like headless or contrib variants.


Ubuntu's python3-opencv package also doesn't know anything about opencv-python names:


/METADATA

Modern Python uses PKG-INFO: https://www.python.org/dev/peps/pep-0314/
Not sure about "conda".

Examples:


tries to install a Python library that depends on opencv-python

Are there another ways to mask package installation in pip?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opencv-python

It is just a name of the package from dedicated repository: https://github.com/opencv/opencv-python
It is not an entity of this upstream repository. Perhaps we should not have it here.

Moreover there are several conflicting subpackages from "opencv-python", like headless or contrib variants.
Ubuntu's python3-opencv package also doesn't know anything about opencv-python names:

Ack, I see why you may not want CMake's upstream to enter in this details that are mostly related to distributions (and mixing two different distribution, the base apt/conda system and pip).

Modern Python uses PKG-INFO: https://www.python.org/dev/peps/pep-0314/

I am not an expert, I got the METADATA info from official Python documentation (and from PyPI's opencv-python installations):

From a quick search, it seems that it was introduced by the following PEPs that amend/complete pep-0314:

tries to install a Python library that depends on opencv-python

Are there another ways to mask package installation in pip?

That I am aware of, just passing to pip the --no-deps, that however do not install any dependency at and needs to be specified manually by users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@traversaro Thank you for information!

I have looked a bit on this specification: https://www.python.org/dev/peps/pep-0345/

There is a field to declare package name aliases (which wold work for us): Provides-Dist
E.g. we could have package name opencv and define known package aliases with names: opencv-python, opencv-python-headless.
But, looks like installation tools don't care about this field: https://stackoverflow.com/questions/61603438/do-any-tools-support-provides-dist-metadata-for-python-packages-if-not-why

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a random dude, who would like this feature to be implemented.

Regarding the package name, how about just making the OPENCV_PYTHON_PIP_METADATA_INSTALL option accept a String instead of a boolean and use that name everywhere opencv_python/opencv-python currently appears (with appropriate _/- translations)? This way, downstream package distributors could choose the appropriate name themselves?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a curiosity, what is the use case for you for using this feature?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use ArchLinux and I normally prefer to install my python packages through pacman/AUR. The official python-opencv arch package builds and installs opencv from source (including the python bindings). This leads to the problems described by @traversaro, where pip doesn't know, that opencv is already installed. Currently, I roll my own PKGBUILD that installs the appropriate dist-info alongside the system-wide opencv package, but this is obviously inconvenient.

@mshabunin
Copy link
Contributor

What is current status of this PR? It should help to make our build visible to pip as opencv-python package, am I correct? How one should use these parameters to achieve this, e.g with venv?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants