سؤال

I followed directions to download WMI for Python here

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

When I attempt to run the code

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 ()

I receive the error

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

EDIT:

I am using Python 2.7.6

EDIT2:

I am running 64-bit Windows 2008 R2, and I downloaded WMI-1.4.9.zip (md5) from https://pypi.python.org/pypi/WMI/. I extracted the contents and saved it in D:\Python\Tools\Scripts. I executed

python setup.py.install

I added D:\Python\Tools\Scripts to %PATH%, and when I execute the code

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 ()

I get the error

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

EDIT3:

I now have Python 2.7.6 and the installation is D:\Python27

I extracted WMI-1.4.9.zip in D drive and executed the following

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

And the output is

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

But when I execute my original code, I get the same error

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

EDIT4:

And I included the following in %PATH%

D:\Python27;D:\Python27\Lib\;D:\Python27\Lib\site-packages
هل كانت مفيدة؟

المحلول

I figured it out.

I needed to install Python for Windows extensions located at http://sourceforge.net/projects/pywin32/files/

Since I am using Python 2.7.6 on 64-bit server I installed pywin32-218.win-amd64-py2.7.exe

نصائح أخرى

One thing that I have found is this:

If you are using python 2, you need to use

python setup.py install

Which will work for python 2, but not 3. In that case, you need to use

python3 setup.py install
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top