Python ImportError: 找不到名为wmi的模块。

5
我按照这里的指示下载了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

你从这里下载了WMI吗?https://pypi.python.org/pypi/WMI/ - Wouldn't You Like To Know
@BenjiSparks - 是的,我从那里下载了。 - Glowie
2个回答

5

我想通了。

我需要安装 Windows 下的 Python 扩展,网址为 http://sourceforge.net/projects/pywin32/files/

考虑到我使用的是 64 位服务器上的 Python 2.7.6 版本,因此我安装了 pywin32-218.win-amd64-py2.7.exe


我正在使用64位Windows 7操作系统,但是只有32位的Python 2.7版本,所以我下载了pywin32-220.win32-py2.7.exe进行安装。 - Benjamin Castor

1

我发现一个问题:

如果您使用的是Python 2,您需要使用

python setup.py install

这对Python 2有效,但不适用于Python 3。在这种情况下,您需要使用

python3 setup.py install

我正在使用Python 2.7.5。 - Glowie
@Glowie 我唯一能建议的就是尝试重新安装它。 - Beta Decay
我重新安装了它,但仍然无法工作。我在原始问题中包含了详细信息。 - Glowie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接