import cv2 module error

:red_question_mark: Deployment Error: ModuleNotFoundError: No module named 'cv2'

1. Description

I’m trying to deploy a Streamlit app that uses OpenCV (cv2). The app works locally but fails on Streamlit Community Cloud with a ModuleNotFoundError. I believe it may be due to Python version compatibility with opencv-contrib-python.


2. Deployment Details

a. Is it deployed on Community Cloud?
:white_check_mark: Yes

b. Public app link:
:link: https://helmet-numberplate-detection.streamlit.app/


3. GitHub Repository

:link: GitHub - lakshmi-755/helmet_number_plate_detection_project: Takes image or video of motorcyclists as input And detects whether they are wearing helmet or not If they they are not wearning helmet then their numberplate is detected Model tries to extract the text on the number plate such that further actions can be taken


4. Full Error Message

vbnet

CopyEdit

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).

Traceback:

File "/mount/src/helemet_detection_numberplate/streamlit_app.py", line 4, in <module>
    import cv2

5. Environment Info

  • Streamlit version: 1.41.1
  • Python version: 3.12.1

6. Additional Info

  • My requirements.txt includes:

nginx

CopyEdit

streamlit
opencv-contrib-python==4.9.0.80
ultralytics
numpy
pillow
easyocr
imutils
  • I also created a packages.txt file with:

nginx

CopyEdit

libgl1

:red_exclamation_mark: Suspected Cause

From logs, it seems Streamlit Cloud is installing a Python version (like 3.13 or 3.12) that is not yet compatible with opencv-contrib-python==4.9.0.80. The package silently fails to install, leading to cv2 not being found.


:light_bulb: What I’ve Tried

  • Verified requirements.txt and packages.txt
  • Rebuilt app
  • Still encountering cv2 import error

:folded_hands: Request

Can someone confirm:

  • Whether Python 3.12/3.13 is supported with opencv-contrib-python
  • If a runtime.txt file (e.g. python-3.11) can help resolve this?

Thank you!

  1. Python 3.x compatible pre-built wheels are provided for the officially supported Python versions (not in EOL): 3.7, 3.8, 3.9, 3.10, 3.11, 3.12. Source: opencv-contrib-python · PyPI
  2. You can set the version of Python (from 3.9 to 3.12) like this:

From the current Pypi page:

b. Packages for server (headless) environments (such as Docker, cloud environments etc.), no GUI library dependencies

These packages are smaller than the two other packages above because they do not contain any GUI functionality (not compiled with Qt / other GUI components). This means that the packages avoid a heavy dependency chain to X11 libraries and you will have for example smaller Docker images as a result. You should always use these packages if you do not use cv2.imshow et al. or you are using some other package (such as PyQt) than OpenCV to create your GUI.

  • Option 3 - Headless main modules package: pip install opencv-python-headless
  • Option 4 - Headless full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python-headless (check contrib/extra modules listing from OpenCV documentation)