本文介绍了如何解决python导入错误-DLL访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have installed a certain python package (netCDF4), which contains compiled code (extension module). I am running Anaconda and python 3.6 under Windows 10 (x64). When importing the module from console, I get the following error:

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 文件被公司范围的安全策略阻止,这是相当严格的.例如,除非在程序文件"文件夹中,否则默认情况下会阻止二进制文件.但是我的 python 发行版和 netCDF4 包已经在这个文件夹中,所以我不知道如何解释这一点.Windows 事件查看器中的 AppLocker 日志未显示任何阻止活动.depencency walker 工具列出的依赖项要么是系统库,要么包含在程序文件"文件夹中.

I suspect that some crucial DLL file is blocked by company-wide security policy, which is quite restrictive. For instance, binary files are blocked by default unless in the "program files" folder. But my python distribution and the netCDF4 package are already within this folder, so I'm at loss how to explain this. The AppLocker log in Windows Event Viewer does not show any blocking activity. The dependencies listed by the depencency walker tool are either system libraries or contained in the "program files" folder.

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

How do I start troubleshooting? How can I find out what is going on?

推荐答案

经过长时间的挣扎,我现在有了解决方案.

After a long struggle, I now have the solution.

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

I started python in admin mode, and used the tool Process Explorer to log which DLL files were loaded. The import netCDF4 statement loaded around 10 extra DLL files. I then started python in user mode, and used ctypes.WinDLL to load each of these libraries manually. I was then able to pinpoint the exact library (hdf5.dll) that caused the problems. It turned out that hdf5.dll had incomplete permissions, so that it could only be loaded with administrator privilegies.

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

Although my problem was very specific, I hope that my solution can help others in related situations...

这篇关于如何解决python导入错误-DLL访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:46
查看更多