Error Again: ModuleNotFoundError: No module named 'cv2'

ModueNotFoundError:No module named ‘cv2’ had been posted with a solution in Oct 2021. The same error seems to has come back. The previous solution didn’t work. So, I’m posting my question here.

The app that used opencv was deployed to streamlit cloud and has been working well for the past several weeks.
I changed some minor wording in the code on Feb 3, 2022 and verified that the app worked on my laptop (i.e. local host).
The streamlit cloud now showed an error “No module named “cv2”” when my app was rebooted and run.

I’ve read the relevant postings in this forum and tried the recommended solutions as follows.
(1) Use opencv-python-headless in requirements.txt
(2) Tried packages.text with

Nothing worked for me. I’m still getting the error message.

The source code can be found:
https://github.com/testruncoder/cv2bugtest/blob/master/streamlit_app.py

The code was deployed to streamlit cloud:
https://share.streamlit.io/testruncoder/cv2bugtest

Here’s the requirements.txt.

streamlit==1.3.0
streamlit-webrtc==0.32.0
numpy==1.19.2
cvzone==1.5.3
opencv-python-headless==4.5.1.48

I’m wondering if there has been any change to libraries or versions on streamlit cloud.

I would greatly appreciate any help to resolve the issue.

Thanks,

Hi @funcode -

If I’m not mistaken, the streamlit-webrtc package was using an internal API that was deprecated, which temporarily broke some apps.

Your requirements.txt is showing 0.32.0; can you try the newest version 0.34.2?

Best,
Randy

Hi Randy,

Thank you for your prompt response to my question.

I’ve changed the version of streamlit-webrtic to 0.34.2 in the requirements.

A different error message read as follows.

ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ (/home/appuser/venv/lib/python3.8/site-packages/cv2/cv2.cpython-38-x86_64-linux-gnu.so)

Attached is the error log from streamlit cloud.

Thanks,

(Attachment logs-testruncoder-cv2bugtest-master-streamlit_app.py-2022-02-07T20 06 12.653Z.txt is missing)

Hi @funcode! I’m helping Randy investigate this issue. From what I can see in the logs, it is now returning an error because it is hitting this issue python - cannot import name '_registerMatType' from 'cv2.cv2' - Stack Overflow .

I’ve forked your repository and deployed your application on Python 3.7 (by accident) and it worked. I’m trying to reproduce this on Python 3.8.

Alex

Hi Alex, thank you for your help.

Just FYI, I’ve always used Python 3.8, not 3.7, in developing my codes. I presume that my previous deployment to streamlit cloud used Python 3.8.
I can’t wait to see this problem resolved.
Thanks,

Could you share what version of pip you are using locally? Could you add a file to lock the dependencies to your repository?

pip 21.3.1 on my laptops.

By the way, I didn’t have this error with streamlit cloud until Feb 3rd, 2022.
I’m wondering what might have been changed with pip on streamlit cloud settings.

Sorry, I’m not a highly experienced programmer and couldn’t understand your question.
What file else do I need to lock the dependencies to the repository?

No worries! :slight_smile: We tried to upgrade multiple apps in Cloud to use the latest base images, and this might have caused some issues for a few existing apps. We are working on identifying the best solution.

Can you try something like this python - Automatically create requirements.txt - Stack Overflow to generate a requirements file.txt file?

I think you meant that the requirements.txt listed all the dependencies.

Requirements.txt has been created locally by ‘pip freeze > requirements.txt’.
It’s a long list and can be viewed in the github link as shown above.

A different error message came out this time:

Error_msg_Error installing requirements

The error log has been downloaded. I’m no sure how to attach the error log text file here.
The last portion of the error log reads as follows.

Collecting pytz-deprecation-shim==0.1.0.post0

  Downloading pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)

ERROR: Could not find a version that satisfies the requirement pywin32==303 (from versions: none)

ERROR: No matching distribution found for pywin32==303

[manager] installer returned a non-zero exit code

[manager] Error during processing dependencies! Please fix the error and push an update, or try restarting the app.

[manager] Streamlit server consistently failed status checks

[manager] Please fix the errors, push an update to the git repo, or reboot the app.

Thanks @funcode ! I will investigate based on the information you provided. Until I get to the root cause, and I know you usually use Python 3.8 and this might lead to other issues, but you can create a branch and deploy to Python 3.7 to avoid this issue.

Thanks, Alex. I’ll go ahead deploy my apps with Python 3.7 in the meantime.

Hello, I’m having the same issue.
This is my repo: GitHub - angelicaba23/app-super-resolution
I already tried with py 3.7, 2.8 and 3.9 and doesn’t work when deploying but local it is great!

face_detection==0.2.1
opencv_python==4.5.5.64
Pillow==9.0.1
streamlit==1.7.0

Have you tried streamlit==1.3.0, opencv-python-headless==4.5.1.48 and python==3.7?

If OpenCV is installed on your system but Python cannot find it, you will get the “no module named ‘cv2’” error message. This can happen if OpenCV is not in the Python path. The Python path is a list of directories where Python looks for modules and packages. You can check the Python path by running the following command in your terminal or command prompt:

python -c "import sys; print(sys.path)"

This will print out the directories in the Python path. Make sure that the directory where OpenCV is installed is included in the Python path.

@linehammer, is your recommended step working on a heroku server? I had no problem with running the code on a local driver. The problem takes place when the code is run from a heroku server.

1 Like

Same issue with me

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.