如何解决 python 导入错误 - DLL 访问被拒绝

How to troubleshoot python import error - DLL access denied

我已经安装了某个python包(netCDF4),其中包含编译代码(扩展模块)。我是 运行 Anaconda 和 python 3.6 Windows 10 (x64)。从控制台导入模块时,出现以下错误:

In [1]: import netCDF4
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-9588a3d4fb24> in <module>()
----> 1 import netCDF4

C:\Program Files\Anaconda3\lib\site-packages\netCDF4\__init__.py in <module>()
      1 # init for netCDF4. package
      2 # Docstring comes from extension module _netCDF4.
----> 3 from ._netCDF4 import *
      4 # Need explicit imports for names beginning with underscores
      5 from ._netCDF4 import __doc__, __pdoc__

ImportError: DLL load failed: Access is denied.

但是模块是从管理员帐户完美加载的。

我怀疑某些关键的 DLL 文件被公司范围的安全策略阻止了,这是非常严格的。例如,二进制文件默认被阻止,除非在 "program files" 文件夹中。但是我的 python 发行版和 netCDF4 包已经在这个文件夹中了,所以我不知道该如何解释。 AppLocker 登录 Windows 事件查看器未显示任何阻止 activity。 depencency walker 工具列出的依赖项要么是系统库,要么包含在 "program files" 文件夹中。

如何开始故障排除?我怎样才能知道发生了什么?

经过长时间的斗争,我现在有了解决方案。

我在管理员模式下启动 python,并使用工具 Process Explorer 记录加载了哪些 DLL 文件。 import netCDF4 语句加载了大约 10 个额外的 DLL 文件。然后我在用户模式下启动 python,并使用 ctypes.WinDLL 手动加载这些库中的每一个。然后我能够查明导致问题的确切库 (hdf5.dll)。原来是hdf5.dll权限不全,导致只能以管理员权限加载。

虽然我的问题很具体,但我希望我的解决方案可以帮助其他处于相关情况的人...

这对我有用:

https://vxlabs.com/2017/12/06/how-to-debug-pyinstaller-dll-pyd-load-failed-issues-on-windows/

When debugging DLL load errors on Windows, use lucasg’s open source and more modern rewrite of the old Dependency Walker software. Very importantly, keep on drilling down through indirect dependencies until you find the missing DLLs.

在此处下载:https://github.com/lucasg/Dependencies

没有管理员权限也可以使用!

这对我有用:关闭防火墙。我用的是360防火墙。在我关掉它之后,一切都很好,花花公子。 希望这有帮助