"No module named win32api"

4722
8
08-04-2010 11:04 AM
PaulHuffman
Occasional Contributor III
I wrote a little python script that checks on the status of services.  It tested fine at my home office, but now that I'm at my field office, the script will not run on the desktops or servers here. As soon as the script tries to import win32api, it throws "No module named win32api".  

However, python for windows extensions seems to be installed. I downloaded the version for python 2.5 from SourceForge and installed it again.  The same version of mfc71.dll is in several directories, including c:\program files\ArcGIS\bin, which is also in the python path.   mfc71.dll is in windows/system32 as well.

Running windows server 2003, ArcGIS 9.3.1, Python 2.5 here.
0 Kudos
8 Replies
DavidWynne
Esri Contributor
Hi Paul,
Another option, is to install PythonWin (not installed with ArcGIS, but is included on the installation media) which would also give you access to win32api (plus win32ui, win32com, etc.).

-Dave
0 Kudos
PaulHuffman
Occasional Contributor III
I finally found out that Pythonwin is installed on these field machines.  In IDLE, I had to add a sys.path.append statement when running under IDLE to find both win32api and win32com.  Not needed when running under pythonwin apparently.  Will I need to append the paths to run this under a windows shortcut?

Now the script throws an error "ImportError: No module named WService" when I run on the field office server, although back at my office it found WService Class just fine in line within the script. What could be the difference between these scripting environments?

Where'd my attachment of the script go?  I'll try to copy in a portion of the script then:

"""
from sys import exit
import time, win32api, win32con, win32service
wa, wc, ws = win32api, win32con, win32service

import WService
iis = WService.WService("IIS Admin")
iis.fetchstatus("stopped")
print "IIS Stopped"

class WService:
    """
    The WService Class is used for controlling WinNT, Win2k & WinXP like
    services. Just pass the name of the service you wish to control to the
    class instance and go from there. For example, if you want to control
    the Workstation service try this:
    
        import WService
        workstation = WService.WService("Workstation")
        workstation.start()
        workstation.fetchstatus("running", 10)
        workstation.stop()
        workstation.fetchstatus("stopped")
0 Kudos
DavidWynne
Esri Contributor
huffmanp;30014 wrote:
Now the script throws an error "ImportError: No module named WService" when I run on the field office server, although back at my office it found WService Class just fine in line within the script. What could be the difference between these scripting environments?

QUOTE]

I'm not familiar with WService (I presume you're working with something like this: http://code.activestate.com/recipes/115875-controlling-windows-services/ ?

In any case, I would compare sys.path between the two machines, and see what the differences are.  Perhaps, it's just a matter of updating PYTHONPATH or one of your .pth files, etc. to see the location of WService.

-Dave
0 Kudos
PaulHuffman
Occasional Contributor III
Yes, that's the WService I found. 

I thought in my testing at my home office that WService is loaded as a class from within the script itself, not as a module python finds in PYTHONPATH. Testing at my home office continues to confuses me.  When I rename both WService.py and WService.pyc that are in the same folder as my testing script, the script runs correctly (in pythonwin).  Can't find any other copies of WService on the system.  That suggests to me that it is reading in WService from within the script.

However, when I renamed the name of the class inside the script to WServicex, at run time the import WService line ran correctly, indicating that it was loading WService from somewhere outside the script. 

Or is it preserved somewhere in memory by pythonwin after the first time I call it? 

PYTHONPATH  at both locations in c:\Program Files\ArcGIS\bin.  The current location of the scripts and WService.py is not within this path.
0 Kudos
PaulHuffman
Occasional Contributor III
Ok, I'm starting to see how this works now.  I think I verified that after WService is loaded, it stays available to subsequent runs of the test script. 

When I renamed WService.py in my script folder and ran the test script, the script accessed WService as if it had never been renamed.  I closed and reopened Pythonwin.  Then the test script could not find WService.  I changed the file name back to WService.py and the test script ran correctly. 

I also tried changing the name and commenting out the WService class that I have in the test script itself.  The test script ran correctly.  So apparently, in pythonwin anyway, the WService class is read in from the same folder as the test script and stays in memory available for other runnings of the script. It looks like a compiled python file WService.pyc is created.  WService.py does not have to be in the PYTHONPATH.

But I still don't understand why at the field office, the script couldn't find win32api and win32con when running from IDLE until I appended the path.  Pythonwin seemed to give my scripts access to win32 with out the appended path.  At my home office, the scripts run fine, find win32api without appending the path from both pythonwin and IDLE.
DavidMuthami1
Occasional Contributor

Awesome. After installing ArcGIS 10.4 i faced  a similar problem and the tweak worked after setting "PYTHOPATH" environment variable

0 Kudos
RandonGregory
New Contributor
Hello,

I'm in the middle of my senior project for a cartography degree and I am stuck with this error.

<type 'exceptions.ImportError'>: No module named win32api
Failed to execute (fetch_jjr).

I am attempting to run a model within ArcMap that required pywin32 extension which I downloaded from sourceforge. The first time i downloaded it I was getting the same error except for win32com.client. I downloaded it again but this time I made sure that it was for my version of python2.5. Now it finds win32com.client but not win32api, I've found the file in site packages but it has the name test_win32api, I tried renaming it and that didn't work.

I have absolutely no experience with python and I have no idea where to go from here.
Any help or suggestions would be greatly appreciated.

Thanks,
0 Kudos
MikeWiniski
New Contributor
I installed pythonwin via the arcdesktop installer, but I was receiving the 'no module named win32 api' when I launched the program.  I had to add the following to the system pythonpath to make it work.

c:\python26;c:\python26\lib\site-packages\;C:\Python26\ArcGIS10.0\Lib\site-packages\

Hope this helps.

Mike
0 Kudos