导入错误:DLL加载失败:找不到指定的模块。

77

我已经安装了Python 2.5.4,Numpy 1.5.0 win32,Matplotlib 1.0.0 win32和pywin32 218。但仍然无法在Python中绘制图形。这是我得到的错误信息:

      import pylab
   File "C:\Python25\lib\site-packages\pylab.py", line 1, in <module>
      from matplotlib.pylab import *
   File "C:\Python25\lib\site-packages\matplotlib\pylab.py", line 216, in <module>
      from matplotlib import mpl  # pulls in most modules
   File "C:\Python25\lib\site-packages\matplotlib\mpl.py", line 1, in <module>
      from matplotlib import artist
   File "C:\Python25\lib\site-packages\matplotlib\artist.py", line 6, in <module>
      from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
   File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in <module>
      from matplotlib._path import affine_transform
ImportError: DLL load failed: The specified module could not be found.

请帮忙一下...


你是如何安装的?看起来一些C扩展没有正确编译/安装。 - tacaswell
对于在Windows上使用科学Python,最好的选择是安装WinPython、Python(x,y)、Enthought Python或Anaconda,而不是尝试手动安装所有内容。这些任何一个都会安装您可能需要的大多数软件包,并应正确配置它们。 - DaveP
4
加载 ...\site-packages\matplotlib\_path.pyd 文件,使用 Dependency Walker 可能会给你一些线索,看看它涉及哪个 DLL 文件。如果都已经正确编译了,这可能是一个路径问题,也许是因为 PATH 变量中首先找到了另一个版本的 DLL 被选中导致的。这个网站提供了 Windows 平台的良好预编译二进制文件: http://www.lfd.uci.edu/~gohlke/pythonlibs/ - Rutger Kassies
谢谢大家回答我的问题,但实际问题是不同的,我已经找到了解决方案。 - DesaiM
https://dev59.com/K1cP5IYBdhLWcg3wXIsy - Shivendra Agarwal
11个回答

26

我在使用Python 3.5.1的Win 64位系统上导入matplotlib.pylab时遇到了同样的问题。通过从以下链接安装Visual Studio 2015的Visual C++ Redistributable程序:https://www.microsoft.com/en-us/download/details.aspx?id=48145,解决了缺少DLL的问题。

我发现这比下载和粘贴DLL更好、更容易。


在Windows 7上,使用来自python.org的Python 3.7.6时,我遇到了与spacy类似的问题。堆栈跟踪如下:File "C:\Users\user_profile\Desktop\new_pro\venv\lib\site-packages\spacy\pipeline_init_.py", line 4, in <module> from .pipes import Tagger, DependencyParser, EntityRecognizer, EntityLinker File "pipes.pyx", line 1, in init spacy.pipeline.pipes ImportError: DLL load failed: 找不到指定的模块。通过上面提供的链接,问题最终得以解决。你救了我的一天! - Pegolon

25

(我从这个视频中找到了答案:http://www.youtube.com/watch?v=xmvRF7koJ5E

  1. 从互联网上下载 msvcp71.dllmsvcr71.dll

  2. 将它们保存到你的 C:\Windows\System32 文件夹中。

  3. 如果你使用的是64位操作系统,也要将它们保存到你的 C:\Windows\SysWOW64 文件夹中。

现在尝试在Python中运行你的代码文件,它将在几秒钟内加载图形。


救了我的一天!但是为什么它会从正确的路径中获取呢? - everlasto
3
如果这不是正确的方法,请请求 @JFFIGK 提供解决方案。 - Sivaram Boina
2
也许这比这更简单?当我在另一个上下文中遇到了相同的错误时,我按照https://dev59.com/K1cP5IYBdhLWcg3wXIsy中@User1024的答案添加了路径变量。 - questionto42
15
请不要相信在某些 YouTube 视频中找到的 DLL 文件 - 这是一种非常危险的解决方案。如果可能的话,请从官方的 Microsoft 来源安装所需的 DLL 文件。 - unddoch
嗨。我尝试了这个,但它并没有解决我的问题。不确定为什么。 - hbstha123
我现在也遇到了news3k包的相同错误,之前使用了几个月并没有做出任何明显的更改(但我得到了不同的回溯)。你是怎么知道缺少msvcp71.dllmsvcr71.dll的呢? - HelloGoodbye

24

对于Windows 10 x64Python

打开Visual Studio x64命令提示符,使用dumpbin命令:

dumpbin /dependents [Python Module DLL or PYD file]

如果您没有安装Visual Studio,可以在其他地方下载dumpbin,或者使用Dependency Walker等其他工具。
请注意,所有其他答案(截至目前)都只是胡乱猜测,而这种方法更接近于带夜视功能的狙击步枪。
案例研究1
  1. I switched on Address Sanitizer for a Python module that I wrote using C++ using MSVC and CMake.

  2. It was giving this error: ImportError: DLL load failed: The specified module could not be found

  3. Opened a Visual Studio x64 command prompt.

  4. Under Windows, a .pyd file is a .dll file in disguise, so we want to run dumpbin on this file.

  5. cd MyLibrary\build\lib.win-amd64-3.7\Debug

  6. dumpbin /dependents MyLibrary.cp37-win_amd64.pyd which prints this:

    Microsoft (R) COFF/PE Dumper Version 14.27.29112.0
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    Dump of file MyLibrary.cp37-win_amd64.pyd
    
    File Type: DLL
    
      Image has the following dependencies:
    
        clang_rt.asan_dbg_dynamic-x86_64.dll
        gtestd.dll
        tbb_debug.dll
        python37.dll
        KERNEL32.dll
        MSVCP140D.dll
        VCOMP140D.DLL
        VCRUNTIME140D.dll
        VCRUNTIME140_1D.dll
        ucrtbased.dll
    
      Summary
    
         1000 .00cfg
        D6000 .data
         7000 .idata
        46000 .pdata
       341000 .rdata
        23000 .reloc
         1000 .rsrc
       856000 .text
    
  7. Searched for clang_rt.asan_dbg_dynamic-x86_64.dll, copied it into the same directory, problem solved.

  8. Alternatively, could update the environment variable PATH to point to the directory with the missing .dll.

请随意添加您自己的案例研究!我已经将其设置为社区维基答案。


看起来像是“readelf -d”。有没有办法从“dumpbin”中查看可能的加载状态信息?类似于“ldd”。 - shs_sf
2
这帮助我解决了问题,但我必须检查我的dll的依赖项(我使用了需要libstdc++、libwinpthread和libgcc的Qtcore)。如果文件夹中没有这些dll,则会导致导入错误。 - f222
@f222 是的,这也可以正常工作。Dumpbin只是检查DLL依赖项的一种方法,还有其他方法。 - Contango
3
这个回答值得更多的赞。它提供了对于任何“ImportError”问题的深入了解,因此在搜索结果中排名第一。 - Burak

8

救命稻草!唯一的简单且可用的解决方案! - Carl Cheung

5

2

快速提示: 检查您是否安装了其他Python版本,如果您已经卸载它们,请确保操作正确。如果您的系统上安装了Miniconda,则Python将不会轻易被删除。

我的解决方法:卸载其他Python版本和Miniconda,重新安装Python和matplotlib库,一切都很顺利。


2

我安装了vc++,这解决了该问题。


1
重新安装相关软件包。
我也遇到了numpy的同样问题。我首先卸载了它:
pip uninstall numpy

然后再次安装

pip install numpy==1.20.1

我需要那个特定的版本。如果您想安装其他numpy版本,则可以忽略==1.20.1

此外,在安装过程中,我收到了诸如以下错误:

statsmodels 0.12.2需要安装patsy>=0.5,但它没有被安装。

我也安装了这些缺失的先决条件。例如,

pip install patsy

0

我经历了这个错误,经过大量的调查后发现:

问题出在使用cuda和标志cuda_with_fast_math=on编译源代码时的Opencv==4.5.1版本上。

我只需重新构建OpenCV并禁用该标志即可。

cuda_with_fast_math

请确保使用的共享库版本与当前版本匹配

对我有效


0

我刚刚卸载了当前的numpy并从this链接安装了一个wheel numpy。

这解决了我的问题。我想我们不应该使用来自随机来源的dll。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接