Python 导入错误 : No module named wmi

标签 python import wmi

我按照说明在这里下载了适用于 Python 的 WMI

http://timgolden.me.uk/python/wmi/index.html

当我尝试运行代码时

import wmi

c = wmi.WMI ()
for s in c.Win32_Service (StartMode="Auto", State="Stopped"):
  if raw_input ("Restart %s? " % s.Caption).upper () == "Y":
    s.StartService ()

我收到错误

Traceback (most recent call last):
  File ".\see_wmi.py", line 1, in <module>
    import wmi
ImportError: No module named wmi

编辑:

我正在使用 Python 2.7.6

编辑2:

我运行的是 64 位 Windows 2008 R2,我从 https://pypi.python.org/pypi/WMI/ 下载了 WMI-1.4.9.zip (md5) .我提取了内容并将其保存在 D:\Python\Tools\Scripts 中。我执行了

python setup.py.install

我将 D:\Python\Tools\Scripts 添加到 %PATH%,当我执行代码时

import wmi

c = wmi.WMI ()
for s in c.Win32_Service (StartMode="Auto", State="Stopped"):
  if raw_input ("Restart %s? " % s.Caption).upper () == "Y":
    s.StartService ()

我得到了错误

Traceback (most recent call last):
  File ".\see_wmi.py", line 1, in <module>
    import wmi
  File "D:\Python\lib\site-packages\wmi.py", line 88, in <module>
    from win32com.client import GetObject, Dispatch
ImportError: No module named win32com.client

编辑3:

我现在有 Python 2.7.6,安装在 D:\Python27

我在D盘解压了WMI-1.4.9.zip并执行了以下命令

PS D:\WMI-1.4.9> python .\setup.py install

输出是

running install
running build
running build_py
creating build
creating build\lib
copying wmi.py -> build\lib
running build_scripts
creating build\scripts-2.7
copying wmitest.py -> build\scripts-2.7
copying wmiweb.py -> build\scripts-2.7
copying wmitest.cmd -> build\scripts-2.7
copying wmitest.master.ini -> build\scripts-2.7
running install_lib
copying build\lib\wmi.py -> D:\Python27\Lib\site-packages
byte-compiling D:\Python27\Lib\site-packages\wmi.py to wmi.pyc
running install_scripts
creating D:\Python27\Scripts
copying build\scripts-2.7\wmitest.cmd -> D:\Python27\Scripts
copying build\scripts-2.7\wmitest.master.ini -> D:\Python27\Scripts
copying build\scripts-2.7\wmitest.py -> D:\Python27\Scripts
copying build\scripts-2.7\wmiweb.py -> D:\Python27\Scripts
running install_data
warning: install_data: setup script did not provide a directory for 'readme.txt' -- installing right in 'D:\Python27'

running install_egg_info
Writing D:\Python27\Lib\site-packages\WMI-1.4.9-py2.7.egg-info

但是当我执行我的原始代码时,我得到了同样的错误

PS D:\SymantecDLP\Protect> python .\see_wmi.py
Traceback (most recent call last):
  File ".\see_wmi.py", line 1, in <module>
    import wmi
  File "D:\Python27\lib\site-packages\wmi.py", line 88, in <module>
    from win32com.client import GetObject, Dispatch
ImportError: No module named win32com.client

编辑4:

我在 %PATH% 中包含了以下内容

D:\Python27;D:\Python27\Lib\;D:\Python27\Lib\site-packages

最佳答案

我想通了。

我需要为位于 http://sourceforge.net/projects/pywin32/files/ 的 Windows 扩展安装 Python

因为我在 64 位服务器上使用 Python 2.7.6,所以我安装了 pywin32-218。win-amd64-py2.7.exe

关于Python 导入错误 : No module named wmi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23373274/

相关文章:

python - Tkinter - 包含按钮的类

java - 导入语句字节码意义

database - 使用虚拟列导出架构

python : how to speed up this file loading

python - 在 python 中将 rgb 转换为 lab 的快速方法

Python 拒绝写入访问,但我有手动访问权限,这可能是什么原因造成的?

npm - 如何将节点模块导入到 slim 组件中

c# - ManagementEventWatcher(WMI) 从远程机器通知事件时出现异常

javascript - JScript/WMI - 如何检查项目的类/类型?

C# WMI 读出 : Win32_PerfRawData_Tcpip_NetworkInterface returns nothing