Skip to content

Conversation

@Atanu484
Copy link

@Atanu484 Atanu484 commented Sep 5, 2025

Import NumPy before OpenCV in platform.py to prevent import/segfault issues (FreeBSD & cross-platform safety)

On FreeBSD, importing OpenCV (cv2) before NumPy can blow up with errors like “numpy.core.multiarray failed to import” or even a segfault. The reason is that OpenCV relies on NumPy under the hood, so if NumPy hasn’t been loaded yet, things can go wrong.

FreeBSD Ports Tree already carries a patch that imports numpy to prevent this. Thought it would not be a bad idea to upstream this

https://github.com/freebsd/freebsd-ports/blob/main/multimedia/py-scenedetect/files/patch-scenedetect_platform.py

Import NumPy before OpenCV in platform.py to prevent import/segfault issues (FreeBSD & cross-platform safety)
@Breakthrough
Copy link
Owner

Breakthrough commented Sep 5, 2025

Thanks for letting me know, I would be happy to upstream to avoid needing a patch! I can coordinate a new Python release with the fix as soon as possible (this should not affect the Windows distribution).

import typing as ty

import cv2
import numpy

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'numpy' is not used.
Copy link
Owner

@Breakthrough Breakthrough left a comment

Choose a reason for hiding this comment

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

I'm curious, has this caused any actual issues? We also import cv2 in __init__.py before this code has run:

# OpenCV is a required package, but we don't have it as an explicit dependency since we
# need to support both opencv-python and opencv-python-headless. Include some additional
# context with the exception if this is the case.
try:
import cv2 as _
except ModuleNotFoundError as ex:
raise ModuleNotFoundError(
"OpenCV could not be found, try installing opencv-python:\n\npip install opencv-python",
name="cv2",
) from ex

That being said, if this does need to be done, I think the patch was applied incorrectly since it looks like numpy is imported after OpenCV is.

@Atanu484
Copy link
Author

Atanu484 commented Sep 5, 2025

Hi! Thanks for taking a look.

I have recently started maintaining this port, and I thought this was the reason why the patch was added earlier.
I have tested the build without the patch without any apparent error. But then, I am not a regular user of this package.

Since OpenCV is being loaded earlier (as shown in the code), I will remove the patch as well. If any of the users reports any issues, we can revisit here again.

@Atanu484 Atanu484 closed this Sep 8, 2025
@Breakthrough
Copy link
Owner

Hi! Thanks for taking a look.

I have recently started maintaining this port, and I thought this was the reason why the patch was added earlier. I have tested the build without the patch without any apparent error. But then, I am not a regular user of this package.

Since OpenCV is being loaded earlier (as shown in the code), I will remove the patch as well. If any of the users reports any issues, we can revisit here again.

Thank you! Feel free to reach out if you encounter any issues after removing the patch, happy to coordinate any fixes. Cheers.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants