Python NameError:名称'ctypes'没有定义

我试图从一个自定义的.dll文件调用一个函数。 但是当我尝试加载我的库SDK.dll时,我得到以下错误。 我正在按照这里find的迹象: Python导入DLL

有谁知道问题是什么? 我只发现MAC环境的这个问题的参考。

>>> from ctypes import * >>> lib = ctypes.WinDLL('C:/Develop/test/SDK.dll') Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> lib = ctypes.WinDLL('C:/Develop/test/SDK.dll') NameError: name 'ctypes' is not defined 

通过from ctypes import *你是从ctypes模块拉本地命名空间的一切,所以你应该直接调用WinDLL

 >>> from ctypes import * >>> lib = WinDLL('C:/Develop/test/SDK.dll') 

另一种(正如NPE提到的,通常更好)的方法是只导入ctypes

 >>> import ctypes >>> lib = ctypes.WinDLL('C:/Develop/test/SDK.dll') 

更改

 from ctypes import * 

 import ctypes 

前者将所有名称从ctypes导入到当前名称空间中。 通常认为这是一个不好的做法,最好避免。