我对python很新。 (Python 2.6)
我正在尝试通过导入wmi模块来列出正在运行的进程。
import wmi
sys.path.append(r'C:\Python26\Lib\site-packages\isapi\test\build\bdis.win32\winexe\temp')
c = wmi.WMI ()
for process in c.Win32_Process ():
print process.ProcessId, process.Name
系统搜索指向在上面代码中append语句中的位置找到win32api。但我仍然得到错误:ImportError:没有名为win32api的模块
实际错误:
Traceback (most recent call last):
File "C:\Documents and Settings\xxxx\Desktop\Python\Practice Code\file.py", line 1, in <module>
import wmi
File "C:\Python26\lib\site-packages\wmi.py", line 88, in <module>
from win32com.client import GetObject, Dispatch
File "C:\Python26\lib\site-packages\win32com\__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: No module named win32api
我安装了win32extensions,确保版本和平台匹配。我正在使用2.6 我还安装了wmi-1.4.6
我的路径:
C:\Documents and Settings\xxxx\Desktop\Python\Practice Code
C:\Python26\Lib\idlelib
C:\WINDOWS\system32\python26.zip
C:\Python26\DLLs
C:\Python26\lib
C:\Python26\lib\plat-win
C:\Python26\lib\lib-tk
C:\Python26
C:\Python26\lib\site-packages
有什么想法吗?
答案 0 :(得分:2)
这应该包含您需要的所有信息:
http://mail.python.org/pipermail/python-win32/2003-December/001482.html
无需使用WMI,只需使用Win32 Extensions。
from win32com.client import GetObject
WMI = GetObject('winmgmts:')
#List all processes
processes = WMI.InstancesOf('Win32_Process')
for process in processes:
print process.Properties_('Name')
#Get a specific process
p = WMI.ExecQuery('select * from Win32_Process where Name="chrome.exe"')
#view all possible properties
for prop in p[0].Properties_:
print prop
#print out PID
print p[0].Properties_('ProcessId').Value
彼得