Python 导入错误:DLL 加载失败:%1 不是有效的 Win32 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14629818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:00:06  来源:igfitidea点击:

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

pythonopencvimporterror

提问by Francis

I have this issue where I try to import cv2 on python and get the following error message.

我有这个问题,我尝试在 python 上导入 cv2 并收到以下错误消息。

>>> 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.

我确实理解有很多关于此的帖子,其中建议包的位数与 python 包不同。

However, everything I am running is 64 bits. I am on win7 64 bits, I have the winpython2.7.3.3, 64 bits distribution, and I compiled opencv in 64 bits with the instruction provided hereand placed the cv2.pyd dll in the Lib/site-packages folder of python.

但是,我运行的所有内容都是 64 位。我在win7 64位上,我有winpython2.7.3.3,64位发行版,我用这里提供的指令编译了64位的opencv ,并将cv2.pyd dll放在python的Lib/site-packages文件夹中。

Unfortunately the suggestion of using the 32 bits version of python isn't working for me anymore as I have to handle numpy arrays too large for 32 bits.

不幸的是,使用 32 位版本的 python 的建议不再适合我,因为我必须处理对于 32 位来说太大的 numpy 数组。

Thanks!!!

谢谢!!!

------ UPDATE

- - - 更新

The only thing missing was to add the new opencv binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable, restart python.

唯一缺少的是将新的opencv二进制文件路径(C:\opencv\build\bin\Release)添加到Windows PATH环境变量中,重启python。

Everything seems to be working fine now!

现在一切似乎都运行良好!

采纳答案by Francis

The ImportError message is a bit misleading because of the reference to Win32, whereas the problem was simply the opencv DLLs were not found.

由于对 Win32 的引用,ImportError 消息有点误导,而问题只是未找到 opencv DLL。

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).

这个问题是通过将 opencv 二进制文件的路径添加到 Windows PATH 环境变量来解决的(例如,在我的电脑上,这个路径是:C:\opencv\build\bin\Release)。

回答by Oladapo Omonayajo

You could try installing the 32 bit version of opencv

您可以尝试安装 32 位版本的 opencv

回答by lukenothing

Or you have to rebuild the cv2 module for win 64bit.

或者你必须为win 64bit重建cv2模块。

回答by Nick Brady

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.

当我遇到此错误时,它在我的计算机崩溃并重新启动后消失了。尝试关闭并重新打开您的 IDE,如果这不起作用,请尝试重新启动计算机。当我收到此错误时,我刚刚安装了库而没有重新启动 pycharm。

Never closed PyCharm first to test because my blasted computer keeps crashing randomly... working on that one, but it at least solved this problem.. little victories.. :).

从来没有先关闭 PyCharm 进行测试,因为我的电脑一直在随机崩溃......正在处理那个,但它至少解决了这个问题......小胜利......:)。

回答by Jhossep Augusto Popayán Avila

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.

您所要做的就是将 cv2.pyd 文件从 x86 文件夹(例如C:\opencv\build\python\2.7\x86\复制C:\Python27\Lib\site-packages\,而不是从 x64文件夹。

Hope that help you.

希望能帮到你。

回答by Mark Allen

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.

我刚刚遇到了这个问题,问题是该软件包曾一度安装在每个用户的软件包目录中。(在 Windows 上。)又名 %AppData%\Python。所以 Python 首先在那里寻找,找到一个旧的 32 位版本的 .pyd 文件,并因列出的错误而失败。不幸的是 pip uninstall 本身并不足以清除它,此时 pip 10.0.1 似乎没有用于卸载的 --user 参数,仅用于安装。

tl;dr Deleting the old .pyd from %AppData%\python\python27\site-packages resolved this problem for me.

tl;dr 从 %AppData%\python\python27\site-packages 中删除旧的 .pyd 为我解决了这个问题。

回答by MartinD

This error can also appear when python versions are mixed:

当python版本混合时也会出现这个错误:

For example if any of the DLL to be loaded has been compiled using python 2.7.16 and you try to import with python 2.7.15 this error ImportError: DLL load failed: %1 is not a valid Win32 application.is thrown.

例如,如果要加载的任何 DLL 是使用 python 2.7.16 编译的,并且您尝试使用 python 2.7.15 导入,ImportError: DLL load failed: %1 is not a valid Win32 application.则会引发此错误。

This is at least what I've found to be the problem in my case.

这至少是我在我的案例中发现的问题。