我正在尝试使用 python 获取系统中的可用内存量。基本上与我可以从 cat /proc/meminfo | grep MemFree
获得的信息相同.
>>> import psutil
>>> psutil.NUM_CPUS # this works fine
2
>>> psutil.virtual_memory() # this fails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'virtual_memory'
我正在使用 python 2.7.3
更新
>>> psutil.__version__
'0.5.0'
最佳答案
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.NUM_CPUS # this works fine
4
>>> psutil.virtual_memory() # this fails
vmem(total=4042084352L, available=1697619968L, percent=58.0, used=3149373440L, free=892710912L, active=2016649216, inactive=835248128, buffers=55672832L, cached=749236224)
>>> quit()
~$ cat /proc/meminfo | grep MemFree
MemFree: 876836 kB
~$ python -c "print 892710912/1024"
871788
~$ python -c "import psutil;print psutil.__version__"
1.1.3
可能您需要运行:
sudo pip install psutil --upgrade
请注意,当您在一种情况下运行 python 而在另一种情况下运行时,您永远不会得到完全相同的答案。
关于python - psutil 模块在 debian 7 上无法完全工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027440/