GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.02k stars 268 forks source link

[wsgi:error]ImportError: DLL load failed while importing cv2 #893

Closed lllittleX closed 1 month ago

lllittleX commented 3 months ago

When I start the Apache service, an error occurs: [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] import cv2\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] File "F:/bushu/code/Python38/Lib/site-packages\\cv2\\__init__.py", line 181, in <module>\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] bootstrap()\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] File "F:/bushu/code/Python38/Lib/site-packages\\cv2\\__init__.py", line 153, in bootstrap\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] native_module = importlib.import_module("cv2")\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] File "F:\\bushu\\code\\Python38\\lib\\importlib\\__init__.py", line 127, in import_module\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] return _bootstrap._gcd_import(name[level:], package, level)\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] ImportError: DLL load failed while importing cv2: \xe6\x89\xbe\xe4\xb8\x8d\xe5\x88\xb0\xe6\x8c\x87\xe5\xae\x9a\xe7\x9a\x84\xe6\xa8\xa1\xe5\x9d\x97\xe3\x80\x82\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] \r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] During handling of the above exception, another exception occurred:\r [Wed Jul 03 13:40:06.664330 2024] [wsgi:error] [pid 12100:tid 1168] [client 127.0.0.1:63659] \r

My httpd.conf configuration is as follows: `# add mod_wsgi.so LoadFile "f:/bushu/code/python38/python38.dll" LoadModule wsgi_module "f:/bushu/code/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd" WSGIPythonPath "F:/bushu/code/Python38/libs;F:/bushu/code/Python38/Lib/site-packages;F:/bushu/code/Python38/DLLs" WSGIPythonHome "f:/bushu/code/Python38" WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias / F:/bushu/code/code/paddleOCR_deploy/paddleOCR_deploy/wsgi.py WSGIPythonPath F:/bushu/code/code/paddleOCR_deploy <Directory F:/bushu/code/code/paddleOCR_deploy>

Require all granted
</Directory>

`

My wsgi.py code is as follows: import os from django.core.wsgi import get_wsgi_application import sys sys.path.append('F:/bushu/code/code/paddleOCR_deploy') virtualenv_dir = 'F:/bushu/code/Python38/Lib/site-packages' sys.path.insert(0, virtualenv_dir) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'paddleOCR_deploy.settings') application = get_wsgi_application()

I want to know why I can't import the cv2 module. It was working fine before deploying to Apache, but now it's not importing. What should I do? Do I need to modify my wsgi.py code?

GrahamDumpleton commented 3 months ago

I am not a Windows user so not sure I can help too much.

Experience says it might be a few things.

Required DLLs are not in a place where Apache process would look.

Apache is running as different service user and that user cannot access all files under your user.

You are loading PHP into same Apache and it pre-loads an incompatible version of an image library, especially can occur if using Anaconda Python.

Finally, from PyPi page for cv2 module.

Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found.?

A: If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed. If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required.

Windows N and KN editions do not include Media Feature Pack which is required by OpenCV. If you are using Windows N or KN edition, please install also Windows Media Feature Pack.

If you have Windows Server 2012+, media DLLs are probably missing too; please install the Feature called "Media Foundation" in the Server Manager. Beware, some posts advise to install "Windows Server Essentials Media Pack", but this one requires the "Windows Server Essentials Experience" role, and this role will deeply affect your Windows Server configuration (by enforcing active directory integration etc.); so just installing the "Media Foundation" should be a safer choice.

If the above does not help, check if you are using Anaconda. Old Anaconda versions have a bug which causes the error, see this issue for a manual fix.

If you still encounter the error after you have checked all the previous solutions, download Dependencies and open the cv2.pyd (located usually at C:\Users\username\AppData\Local\Programs\Python\PythonXX\Lib\site-packages\cv2) file with it to debug missing DLL issues.

lllittleX commented 3 months ago

Thank you very much for your reply. My Apache is running under administrator privileges, and I am using the PSF Python environment. I also checked the cv2.pyd file based on the dependencies you mentioned, and there was no problem. I wonder if there is any other way to see the DLLs that Apache is referencing.

GrahamDumpleton commented 3 months ago

DLLs on Windows and working out how it is resolving them is where I lack the knowledge.

For a start, I would suggesting dropping back to a single file WSGI hello world application which imports cv2 explicitly and at least take Django out of the picture.

Then look at cv2 Python code to work out import for C extension and import it explicitly.

Don't whether it may be:

import _cv2

or something else.

lllittleX commented 3 months ago

Hello, I followed your method of checking and he can import cv2 instead of _cv2 now. But I found that there is no OpenCV dynamic library in my lib/site-packages/cv2 folder (the format is similar to opencv_worldXXX.dll). I want to ask if this could be the reason for the import failure?

GrahamDumpleton commented 3 months ago

What is the list of files names and sub directories under the lib/site-packages/cv2 package directory? Is there any dll or pyo files in the lib/site-packages directory which may be related to cv2?

GrahamDumpleton commented 3 months ago

Also, what DLLs are in the Python bin directory?

Sometimes DLLs are placed in the bin directory and rely on fact the directory is specified in Path to be found. This may be the problem in that Apache service doesn't have that bin directory in its' Path.