Support python 3.12
Currently, opencv-python fails to build under python 3.12. This patch fixes that problem by permitting the use of any setuptools>=59.2.0, rather than mandating that specific version.
This patch fixes #988. See discussion there for details.
I have tested by successfully installing opencv-python.
This should close https://github.com/opencv/opencv-python/issues/993 as well, and possibly https://github.com/opencv/opencv-python/issues/1005
I tried installing OpenCV on macOS in a virtual environment, with the suggestion by @Avasam of setuptools==59.2.0 and setuptools>59.2.0,<71.0.0, and the installation fails:
Collecting setuptools<71.0.0,>59.2.0 (from -r requirements.txt (line 2))
Using cached setuptools-70.3.0-py3-none-any.whl.metadata (5.8 kB)
Collecting opencv_python>=4.5.2.52 (from -r requirements.txt (line 4))
Using cached opencv-python-4.10.0.84.tar.gz (95.1 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
...
File "/private/var/folders/vp/8rjwz51s5wx5dptcn89czk8m0000gn/T/pip-build-env-h9pxukyj/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 10, in <module>
import distutils.core
ModuleNotFoundError: No module named 'distutils'
I'm on macOS 11.7.10, Python 3.12.5. I upgraded to Python 3.12.6 and have the same issue. I tried in a new virtual environment, and I tried it twice.
Thank you for testing, @mm3509. I run Linux, so I can't confirm whether the platform has any relevance here. But it's worth noting that pip used a cached setuptools. Does it succeed if you use pip's --no-cache-dir option? Relevant discussions online can be found here and here.
Thank you for testing, @mm3509. I run Linux, so I can't confirm whether the platform has any relevance here. But it's worth noting that pip used a cached setuptools. Does it succeed if you use pip's
--no-cache-diroption? Relevant discussions online can be found here and here.
The option --no-cache-dir didn't help, but I tried on Python 3.11 and now it works.
@mm3509 I'm glad you were able to install opencv-python.
N.B. Installation under python 3.11 always has worked. This patch aims to support 3.12. It will be helpful to determine whether this fix works under macOS, and if not what does.
Thank you Bryan. You can count on me to test any patches on macOS :) At the moment, I am out of ideas for how to make it work.
FYI this PR does appear to work for getting opencv-python to install (in Home Assistant's "wheels" container which is based on Alpine Linux and Python 3.13); took me 35 minutes on a Intel CPU from 2021.
$ docker run --rm --entrypoint="" -it ghcr.io/home-assistant/wheels/amd64/musllinux_1_2/cp313:dev bash
75c590f38fa7:~# git clone --depth 1 -b 988-support-py3.12 https://github.com/bryankaplan/opencv-python.git
75c590f38fa7:~# cd opencv-python
75c590f38fa7:~/opencv-python# pip install .
Processing /root/opencv-python
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: numpy>=1.17.0 in /usr/local/lib/python3.13/site-packages (from opencv-python==4.10.0+11cb9fc) (2.2.0)
Building wheels for collected packages: opencv-python
Building wheel for opencv-python (pyproject.toml) ... done
Created wheel for opencv-python: filename=opencv_python-4.10.0+11cb9fc-cp313-cp313-linux_x86_64.whl size=30913164 sha256=d89fdf5385bf01cc750f27f2e3237e858d7f5a6528233a4f62e25370aade0559
Stored in directory: /tmp/pip-ephem-wheel-cache-l0c1rv86/wheels/bb/2f/81/e07df07d397d712a66db6e36035f9b1cfb2048a44317e91e93
Successfully built opencv-python
Installing collected packages: opencv-python
Successfully installed opencv-python-4.10.0+11cb9fc
@bryankaplan or @asmorkalov , could you retrigger this PR's CI? (closing & reopening should work), I don't recall the reason for test failure and I'm guessing it doesn't have to do with this PR.
Even if this doesn't fix everyone's issues. It should be an objective improvement.
I don't recall the reason for test failure and I'm guessing it doesn't have to do with this PR.
Same failure as before:
This request was automatically failed because there were no enabled runners online to process the request for more than 1 days.
Ah so just opencv's self-hosted runenrs being unavailable (opencv-cn-lin-arm64 and python-macos11-m1)
Replaced by https://github.com/opencv/opencv-python/pull/1061.