Python 导入错误:导入安装在 conda 虚拟环境中的 Numpy 时 DLL 加载失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36778066/
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-19 18:19:23  来源:igfitidea点击:

ImportError: DLL load failed when importing Numpy installed in conda virtual environment

pythonpython-2.7numpyanaconda

提问by mkrems

In Windows, I created a Conda virtual environment with the command

在 Windows 中,我使用命令创建了一个 Conda 虚拟环境

conda create -n test python=2.7 pandas scipy matplotlib numpy

Once it is created, I activated the virtual environment and went into a python interpreter. When trying to import numpy, I get the following error:

创建后,我激活了虚拟环境并进入了 python 解释器。尝试导入 numpy 时,出现以下错误:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\test\lib\site-packages\numpy\__init__.py", line 180, in <module>
    from . import add_newdocs
  File "C:\Anaconda3\envs\test\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Anaconda3\envs\test\lib\site-packages\numpy\lib\__init__.py", line 8,
 in <module>
    from .type_check import *
  File "C:\Anaconda3\envs\test\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Anaconda3\envs\test\lib\site-packages\numpy\core\__init__.py", line 14, in <module>
    from . import multiarray
ImportError: DLL load failed: The specified module could not be found.

Any ideas what is going on here? Thanks!

任何想法这里发生了什么?谢谢!

回答by Victor O

Uninstall and install numpy again.

卸载并再次安装numpy。

pip uninstall numpy
pip install numpy

Then try import again, it should work. That is what I did

然后再次尝试导入,它应该可以工作。这就是我所做的

回答by Cardin Lee JH

Unlike @Rafael, for me, libiomp5md.dllwasn't the issue. I installed Dependency Walker to investigate what was going on. Even though the dll versions were different, but Dependency Walker said it was okay.

与@Rafael 不同,对我来说,libiomp5md.dll这不是问题所在。我安装了 Dependency Walker 来调查发生了什么。尽管dll版本不同,但Dependency Walker说没问题。

What was wrong though, was that mkl_intel_thread.dllhad warnings (red icon). If you're using Win 8++, ignore the api-winand ext-msissues as Dependency Walker wasn't updated for new Windows versions and doesn't recognise Windows new APIs.

但是有什么不对,是mkl_intel_thread.dll有警告(红色图标)。如果您使用的是 Win 8++,请忽略api-winext-ms问题,因为 Dependency Walker 未针对新的 Windows 版本进行更新,并且无法识别 Windows 的新 API。

My solution is to copy all mkl_*.dllsfrom the former to the latter:

我的解决方案是将所有内容mkl_*.dlls从前者复制到后者:

  • \Anaconda2\Library\bin
  • \Anaconda2\Lib\site-packages\numpy\core
  • \Anaconda2\Library\bin
  • \Anaconda2\Lib\site-packages\numpy\core

I was able to import numpy and sklearn after that.

之后我能够导入 numpy 和 sklearn 。

回答by cfh

It seems the proper way to fix this is to do:

解决这个问题的正确方法似乎是:

conda install msvc_runtime

If you are in a virtual environment, add this package there.

如果您在虚拟环境中,请在那里添加此包。

回答by HAltos

For my case, I have also the import numpy DLL error in anaconda3, and Decrayer (Nov 28) provided the best solution: I had the same error except for Anaconda3: just added the path \Anaconda3\Library\bin to your Windows 10 path variable and then it worked.

就我而言,我在 anaconda3 中也有 import numpy DLL 错误,而 Decrayer(11 月 28 日)提供了最佳解决方案:除 Anaconda3 外,我遇到了相同的错误:刚刚将路径 \Anaconda3\Library\bin 添加到您的 Windows 10 路径变量然后它起作用了。

I have been trying to install anaconda3.7.1 or 3.7.0 without luck, going to full length of restarting the PC after the key steps to make sure the DLL is no longer in use in memory. Ultimately, thanks to decrayer, now numpy works for me!

我一直在尝试安装 anaconda3.7.1 或 3.7.0 没有运气,在关键步骤之后重新启动 PC 以确保内存中不再使用 DLL。最终,感谢 decrayer,现在 numpy 对我有用!

回答by Rafael Monteiro

For the record, I had the same error here (Python 3.5 64-bit on Windows 10), and this pagehelped me find the solution. The problem was a conflict with libiomp5md.dll, which existed on two locations:

作为记录,我在这里遇到了同样的错误(Windows 10 上的 Python 3.5 64 位),此页面帮助我找到了解决方案。问题是与libiomp5md.dll存在于两个位置的 冲突:

  • C:\Windows\System32\libiomp5md.dll
  • C:\Anaconda3\Library\bin\libiomp5md.dll
  • C:\Windows\System32\libiomp5md.dll
  • C:\Anaconda3\Library\bin\libiomp5md.dll

Python was trying to use the version in System32 folder, which was an old version. I removed it (renamed) and now it uses the correct version, on Anaconda3 folder, and now I can import numpy without the import error.

Python 试图使用 System32 文件夹中的版本,这是一个旧版本。我删除了它(重命名),现在它在 Anaconda3 文件夹中使用了正确的版本,现在我可以导入 numpy 而不会出现导入错误。

回答by BDeforce

For me this was solved by adding the following paths to my system path variable.

对我来说,这是通过将以下路径添加到我的系统路径变量来解决的。

C:\Users\UserName\Anaconda3\
C:\Users\UserName\Anaconda3\bin
C:\Users\UserName\Anaconda3\Scripts
C:\Users\UserName\Anaconda3\Library\mingw-w64\bin (not strictly necessary)
C:\Users\UserName\Anaconda3\Library\bin

Before doing this, you could test this by adding these paths manually:

在执行此操作之前,您可以通过手动添加这些路径来测试:

base_path = r"C:\Users\UserName\Anaconda3"
path = os.pathsep.join([os.path.join(base_path, i) for i in [r"", r"bin", r"Scripts", r"Library\mingw-w64\bin", r"Library\bin"]])
os.environ["PATH"]+=os.pathsep+path

Thanks to this poston PyCharm support.

感谢这篇关于 PyCharm 支持的帖子