本文介绍了在 virtualenv 中加载本机 Windows python 模块时,如何诊断 ImportError: DLL load failed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Python 3.x 和一个 virtualenv——不是 conda,只是一个普通的 virtualenv.我激活 venv 并运行 pip install opencv-python.然而,import cv2 给我一个 DLL not found 错误:

I am using Python 3.x and a virtualenv -- not conda, just a plain virtualenv.I activate the venv and run pip install opencv-python. However,import cv2 gives me a DLL not found error:

(tf) C:\>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\src\venv\tf\lib\site-packages\cv2\__init__.py", line 4, in <module>
    from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
>>>

这是一个 virtualenv 错误吗?如何确定缺少哪个模块/dll?

Is this a virtualenv bug? How do I figure out which module/dll is missing?

推荐答案

关于解决找不到模块"的一般错误

尝试使用 Microsoft 的 Dependency Walker 或 lucasg 的 Dependencies 加载模块.请务必从您的 virtualenv 的命令提示符运行 Dependencies.exe,以便它选择您修改后的 PATH.

Try using either Microsoft's Dependency Walker or lucasg's Dependencies on the module being loaded. Be sure to run Dependencies.exe from your virtualenv's command prompt, so it picks up your modified PATH.

导入行是from .cv2 import *,所以加载的模块和__init__.py在同一个目录下(这是开头的.) 并命名为 cv2-SOMETHING.pyd(这就是原生 Python 模块的样子).将该文件加载到 Dependencies.exe 中,它会向您显示 Windows 想要但找不到的 DLL.

The import line is from .cv2 import *, so the module being loaded is in the same directory as __init__.py (this is the leading .) and named cv2-SOMETHING.pyd (this is what native Python modules look like). Load that file into Dependencies.exe and it will show you the DLL that Windows wants but can't find.

在这种情况下,DLL 是 Python3.dll.为什么不见了?由于 一个 virtualenv 错误 已修复,但尚未进入发布 -- 一年多没有发布了.

In this case, the DLL is Python3.dll. Why is it missing? Because of a virtualenv bug that is fixed, but hasn't made its way into a release -- there hasn't been a release in more than a year.

特别是解决这个问题

github 问题建议修复:使用 venv.

The github issue suggests a fix: use venv.

或者,您可以手动将丢失的 python3.dll 复制到您的 virtualenv 中.您必须为您创建的每个 virtualenv 执行此操作.

Alternatively you can copy the missing python3.dll into your virtualenv by hand. You'll have to do this for every virtualenv you create.

copy "c:\Program Files\Python36\python3.dll" "c:\src\venv\tf\Scripts\"

这篇关于在 virtualenv 中加载本机 Windows python 模块时,如何诊断 ImportError: DLL load failed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:32