python, windows 7, can't import cv2 - python

I know this subject has been brought up before, I've tried all the fixes I've found to no avail. When i try to import cv2 i get the message:
DLL load failed: %1 is not a valid Win32 application.
I'm running python 2.7 on windows 7, 64bit. I've tried installing cv2 using:
python -m pip install cv2
as well as installing the .whl file (opencv_python-2.4.12-cp27-none-win_amd64.whl) the same way. Both times, cmd prompt tells me they've installed correctly, but I still can't import cv2.
The next thing i tried was downloading the latest version of opencv and following the directions here to install it.
I also tried including the paths to the .pyd file in my environment variables:
c:\opencv\build\python\2.7\x64; c:\Python27\Lib\site-packages
I still get the error upon trying to import cv2:
DLL load failed: %1 is not a valid Win32 application.
next, i tried the solved fix proposed in this thread next, I've tried the solved fix proposed in this thread ImportError: DLL load failed: %1 is not a valid Win32 application
However, i had no opencv binaries path that looked like (C:\opencv\build\bin\Release) so i added two paths, c:\opencv\build\x64\vc12\bin and c:\opency\build\vc11\bin to my PATH environment variables. However, this has no effect either and i still get the error upon trying to import cv2. what could i be doing wrong?

Have you tried the wheel from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv ?
There is a 2.4.13 available, maybe that will fix things?

Related

Python: Why does a DLL memory error occur when just importing CV2 on gpu-enabled interpreter?

I have searched far and wide but cannot find a solution to this problem. I am currently running a GPU enabled interpreter (Python v3.8) using CUDA/CUDNN on Pycharm. I am simply trying to import cv2 (v4.5.3.56) and I am getting one constant error:
DLL load failed while importing cv2: Not enough memory resources are
available to process this command.
Apparently the error happens in cv2/init.py folder on this line:
from .cv2 import *
I am guessing the problem is this DLL file here as it is the only one in the cv2 library directory: opencv_videoio_ffmpeg453_64.dll
I have tried updating my PATH variables, dragging a python3.dll file into Anaconda, conda/pip install all kinds of opencv/cv2 version. None of this works. I see one other questions similar to this one but no answer. Also, I should note that all I am doing is importing the library. Nothing else.
DLL load failed while importing cv2 Not enough memory resources are available to process this command both have this issue.

PyInstaller won't import pywin32 / win32clipboard - ImportError upon running executable

I'm working in Windows 10 with Python 3.8.6 and using PyInstaller 4.0 to compile my script as an executable for distribution. I just added a feature today that required importing win32clipboard. PyInstaller finishes compiling without any errors, but the excecutable fails to load due to:
ImportError: DLL load failed while importing win32clipboard: The specified module could not be found.
I attempted to compile the program again using the hidden-import flag:
pyinstaller myscript.py --onefile --hidden-import win32clipboard
This produced the same result and an ImportError upon trying to load the program (no errors during compiling).
I know that win32clipboard is part of pywin32 and my program compiled and ran without any issues prior to the code changes that required importing it. It still runs fine out of IDLE and functions as intended when using the win32clipboard-enabled features.
Is there some way to manually direct PyInstaller to import this correctly, or some other way to fix this issue and get the executable working again?
I was able to work around this issue by importing pywintypes into my script before win32clipboard.
import pywintypes
import win32clipboard
Found the suggestion in an old GitHub bug report for an issue people were having importing win32api with PyInstaller and decided to give it a try. I was able to compile and run my program without any issues after doing this.
Working with PyInstaller: 5.1, Python: 3.8.12 (conda), and Platform: Windows-10.
In addition to the solution from #DJT of importing pywintypes before win32clipboard, I solved the same error by downgrading my pywin32 from version 228 to version 225. Just type the following command with the building environment active:
pip install --upgrade --user pywin32==225

Open CV import CV2 error in Pycharm "ImportError: DLL load failed: The specified module could not be found."

I am attempting to use OpenCV and have the most up-to-date installation of OpenCv downloaded and am unable to install/import it either in Pycharm or using my command prompt in my Windows system. I receive the following error from both: "ImportError: DLL load failed: The specified module could not be found." I looked at some folks that had similar issues and made sure I added the "C:\opencv\build\bin" to my path in environment variable. However I still receive the same error. Happy to answer any questions to help. Thank you.
EDIT:
I got frustrated so removed python and then re-installed; now face the following error: "Could not find a version that satisfies the requirement cv2 (from versions: )
No matching distribution found for cv2"
I tried the below solutions prior to taking these steps. As always appreciate any and all help. Thank you so much

Error importing mahotas in python (missing dll file)

I installed the mahotas library as the site says to but i keep getting the same error when i try to import the module:
Could not import submodules (exact error was: DLL load failed: The specified module could not be found.).
There are many reasons for this error the most common one is that you have
either not built the packages or have built (using `python setup.py build`) or
installed them (using `python setup.py install`) and then proceeded to test
mahotas **without changing the current directory**.
Try installing and then changing to another directory before importing mahotas.
Someone knows what i'm doing rong?
You seem to be on Windows.
For that platform, I recommend you try these compiled packages: http://www.lfd.uci.edu/~gohlke/pythonlibs/
(I'm the author of mahotas)

Error "ImportError: DLL load failed: %1 is not a valid Win32 application"

I have this issue where I try to import cv2 on Python and get the following error message.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 is not a valid Win32 application.
I do understand there are many posts about this where it is suggested that the bitness of the package is different from the Python package.
However, everything I am running is 64 bits. I am on Windows 7 64 bits, I have the winpython 2.7.3.3, 64 bits distribution, and I compiled OpenCV in 64 bits with the instruction provided here and placed the cv2.pyd DLL file in the Lib/site-packages folder of the Python interpreter.
Unfortunately, the suggestion of using the 32 bits version of Python isn't working for me any more as I have to handle NumPy arrays too large for 32 bits.
The only thing missing was to add the new NumPy binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable, restart the Python interpreter.
Everything seems to be working fine now!
The ImportError message is a bit misleading because of the reference to Win32, whereas the problem was simply the OpenCV DLLs were not found.
This problem was solved by adding the path the OpenCV binaries to the Windows PATH environment variable (as an example, on my computer this path is: C:\opencv\build\bin\Release).
You could try installing the 32 bit version of OpenCV.
I just hit this and the problem was that the package had at one point been installed in the per-user packages directory. (On Windows.) aka %AppData%\Python. So Python was looking there first, finding an old 32-bit version of the .pyd file, and failing with the listed error. Unfortunately pip uninstall by itself wasn't enough to clean this, and at this time pip 10.0.1 doesn't seem to have a --user parameter for uninstall, only for install.
tl;dr Deleting the old .pyd from %AppData%\python\python27\site-packages resolved this problem for me.
Or you have to rebuild the cv2 module for Windows 64 bit.
This error can also appear when Python versions are mixed:
For example, if any of the DLL file to be loaded has been compiled using Python 2.7.16 and you try to import with Python 2.7.15 the error ImportError: DLL load failed: %1 is not a valid Win32 application. is thrown.
This is at least what I've found to be the problem in my case.
All you have to do is copy the cv2.pyd file from the x86 folder (C:\opencv\build\python\2.7\x86\ for example) to C:\Python27\Lib\site-packages\, not from the x64 folder.
I had the same error as the one mentioned in previous answers, but it happens only when I use pyinstaller.
I did the following in my Poetry venv:
poetry run python -m pip install pypiwin32
And it worked for me. I am using
python 3.8.10
pyinstaller 4.7
# Those are the versions that worked for me
pypiwin32 223
pywin32 303
pywin32-ctypes 0.2.0
When I had this error, it went away after I my computer crashed and restarted. Try closing and reopening your IDE. If that doesn't work, try restarting your computer. I had just installed the libraries at that point without restarting PyCharm when I got this error.
I never closed PyCharm first to test, because my blasted computer keeps crashing randomly... I am working on that one, but it at least it solved this problem... little victories... :).

Categories

Resources