试图理解错误 - wintypes.Structure

时间:2014-03-09 18:59:01

标签: python spyder

我需要帮助才能弄清楚这件事 昨天我在我的Win7 64位笔记本电脑上安装了Spyder 2.3测试版与Anaconda Python 3.3一起使用。 打开一个内部控制台窗口,我注意到每秒触发的错误,

Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\spyderlib\widgets\status.py", line 80, in update_label
  self.label.setText('%d %%' % self.get_value())
File "C:\Miniconda3\lib\site-packages\spyderlib\widgets\status.py", line 93, in get_value
  return memory_usage()
File "C:\Miniconda3\lib\site-packages\spyderlib\utils\system.py", line 20, in windows_memory_usage
  class MemoryStatus(wintypes.Structure):
AttributeError: 'module' object has no attribute 'Structure'

这似乎来自system.py在未安装wintype.Structure时尝试使用“psutil”的事实,我看不到像Structure类(或模块)这样的东西)wintypes.py ... {偶数c_uint64不在wintypes

我以简单的方式解决了这个问题 - 我刚刚安装了psutil并完成了。但事实是,有几个脚本似乎使用wintypes.Structure(请参阅Google)。我真的不明白......所以我错过了什么? 谢谢!

1 个答案:

答案 0 :(得分:0)

我正在编辑我的答案。

这里的问题似乎是wintypes.py的Python 2.7版本(来自ctypes 1.1.0软件包)以
开头 from ctypes import *

而来自Python 3.3 ctypes 1.1.0软件包的wintypes.py使用:
import ctypes

所以基本上wintypes.Structure.c_uint64.sizeof.byref都来自ctypes。它们不会在wintypes中被改变 - 至少不会达到1.1.0版本。因此,对Spyder(2.3.0beta3)的status.py进行的更改应该可以使Python 2.7和Python 3.3都没有错误:

def windows_memory_usage():
    """Return physical memory usage (float)
    Works on Windows platforms only"""
    from ctypes import windll, Structure, c_uint64, sizeof, byref
    from ctypes.wintypes import DWORD
    class MemoryStatus(Structure):
        _fields_ = [('dwLength', DWORD),
                    ('dwMemoryLoad',DWORD),
                    ('ullTotalPhys', c_uint64),
                    ('ullAvailPhys', c_uint64),
                    ('ullTotalPageFile', c_uint64),
                    ('ullAvailPageFile', c_uint64),
                    ('ullTotalVirtual', c_uint64),
                    ('ullAvailVirtual', c_uint64),
                    ('ullAvailExtendedVirtual', c_uint64),]
    memorystatus = MemoryStatus()
    # MSDN documetation states that dwLength must be set to MemoryStatus
    # size before calling GlobalMemoryStatusEx
    # http://msdn.microsoft.com/en-us/library/aa366770(v=vs.85)
    memorystatus.dwLength = sizeof(memorystatus)
    windll.kernel32.GlobalMemoryStatusEx(byref(memorystatus))
    return float(memorystatus.dwMemoryLoad)
相关问题