Forums

Flask app cannot import cv2

Hi! When import OpenCV in my flask app, there's no error after saving the file, but when accessing the website, there's is an error says that No module named cv2.

I am using Python3, and I've installed opencv-python using pip3 at the online editor console.

Requirement already satisfied: opencv-python in /home/sichen/.local/lib/python3.7/site-packages (4.1.2.30) Requirement already satisfied: numpy>=1.14.5 in /usr/lib/python3.7/site-packages (from opencv-python) (1.17.3)

Your website is using Python 3.8, so you should use pip3.8 to install packages for it. See this help page for more information.

Thanks a lot!

Greetings, I'm trying to install cv2 and I'm getting the following error: 00:12 ~ $ pip3.8 install cv2 Looking in links: /usr/share/pip-wheels ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none) ERROR: No matching distribution found for cv2 Any suggestions? Thanks in advance.

I just tried a different method and got "Error No. 13". Can someone tell me what that means?

00:27 ~ $ pip3.8 install opencv-python Looking in links: /usr/share/pip-wheels Collecting opencv-python Using cached https://files.pythonhosted.org/packages/0b/61/843ab00a3ed67f3f50be786bd9c78ff52c55841a13f26f8cb3cd8502eb09/opencv_python-4.2.0.34-cp3 8-cp38-manylinux1_x86_64.whl Requirement already satisfied: numpy>=1.17.3 in /usr/lib/python3.8/site-packages (from opencv-python) (1.17.3) Installing collected packages: opencv-python ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python3.8/site-packages/cv2' Consider using the --user option or check the permissions.

Have a look at our help page about installing modules: https://help.pythonanywhere.com/pages/InstallingNewModules/

Hi I use flask too and I set up my web app manually. I have an import error in the error log:

Error running WSGI application ModuleNotFoundError: No module named 'cv2'
File "/var/www/myapp_pythonanywhere_com_wsgi.py", line 102, in module

from flask_server import app as application

File"/home/my_project/my_server/flask_server.py", line 6, in module

  import cv2

f you're seeing an import error and don't know why, we have a dedicated help page to help you debug: https://help.pythonanywhere.com/pages/DebuggingImportError/

It's weird since the app works fine when i launch it on bash using my virtual env like this "python -i /var/www/myapp_pythonanywhere_com_wsgi.py ", I can also see the right paths printing sys.path. I obviously specified my virtual env path in the web tab section and reloaded it. I even added a sys.path.append(cv2_path) in the WSGI file of my web app, but it keeps giving this error. Any ideas?

ps. I had to compile my OpenCv library in my virtual env to use contrib modules

That's very odd. Can we take a look at your files? We can see them from our admin interface, but we always ask for permission first.

The problem seems to be solved. I went though the procedure for installing modules and everything was OK.

@giles sure go ahead, i appreciate your help!

@Artscorer still not working on my web app, lol.

my objective right now is to solve the cv2 import on my web app, because I think I can handle the rest of the debug hopefully.

just to double check- when you did a python wsgi.py, from within your virtualenv, did it run correctly with no import errors? or did it have the same import errors? if it has the same errors, I would try ot make sure that yo have the opencv stuff installed correctly in your virtualenv.

Something weird happened: I double checked as @conrad suggested, and my virtual env wasn't actually working. And that's weird because typing "env" I checked that , idk how, some of my env variables were actually deleted, precisely "PYTHONPATH" and "LD_LIBRARY_PATH". I added them again and now it starts, tries to load my models and presents me a python console, even tho it should start the web app. Anyway i reloaded the web app and tried once again to connect to it via browser and the error log gives me the import cv2 error again. I'm not understanding what I did wrong, cause now in my virtualenv I can import cv2 without problems.

ps. I do know why it presents me with the python console after loading my models. Using bash console it actually doesn't start my web app because there is the

if __name__ == '__main__':

do stuff to start the web app...

so I would say it works on my console, but not the hosted web app

[edit by admin: formatting]

Just for clarity, env isn't related to virtualenvs -- it's how you look at Bash environment variables. (Perhaps that's something you're aware of, but I wanted to make sure that any other readers also understand.)

When you say that you can import cv2 in your virtualenv, could you give the exact steps that you're doing to confirm that? I can see that you have two virtualenvs, one called cv2 (for Python 3.6) and one called opencv (for Python 2.7), and neither of them has cv2 installed.

Guys I solved the problem. It was my mistake, I needed to sym-link opencv cv2.so bindings into my virtual environment. Since i compiled and installed opencv manually I needed to link it manually too. Now everything works just fine.

This is a good guide to install opencv manually if some of you are interested:

https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/

Also, tip for those who want to compile and install libraries using cmake on pythonanywhere, make sure that your CMAKE_INSTALL_PREFIX variable is set to a folder in /home/myproject because you don't have root privileges and it set on /usr/lib by default (i know lots of you already know this and probably have better solutions, but I struggled to understand this infos as a noob and it may be helpful to some).

Btw thank you for your help, it was useful, GG!

Great! Thanks for the helpful post.

how can i reach a webcam from using cv2.VideoCapture(0) from pythonanywhere? It is that possible? can anyone help me?

See this forum post.