views:

617

answers:

5

I'm trying to use some python-2.1 code to control another program (ArcGIS). The version of python I am using is 2.5. I am getting the following error message when I run the code.

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

I tried installing pywin32-214.win32-py2.5.exe but I still get the same error message. I can't figure out if I need to do anything to my original python install so it knows that I have installed this.

I think the problematic part of my code is the following:

import win32com.client, sys, string, os, re, time, math

gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
conn = win32com.client.Dispatch(r'ADODB.Connection')

Thanks for your help - I am quite new to python.

A: 

print out sys.path right before the import and make sure the path to win32com is in there

gnibbler
win32com is not there, even if I import it beforehand. win32 is though.
womble
A: 

What version of ArcGIS are you using?

Also is it the same version of ArcGIS you built the old script around?

Also ESRI installs their own version of python from the get go for ArcGIS. Try launching the python executable from the ArcGIS\bin folder.

ESRI changed certain parts of their Geoprocessor library, we had to do some serious rewriting of certain scripts to function properly with ArcGIS 9.3.

Have you tried using the new syntax of ArcGIS 9.3 for python?

UberJumper
I'm using ArcGIS 9.3, which is a different version. The script was built for v9.1
womble
A: 

Hello hello ... everybody please read the error message: "No module named win32api" i.e. not win32com

Put some diagnostic stuff up the top of your script, e.g.

import sys
print sys.version
print sys.path
print sys.argv[0]

and cut the rest of your script down to the minimum necessary to reproduce the problem.

Show us (a) the script (b) the output, exact traceback and error message, reproduced by copy/paste into and edited version of your question i.e. don't retype any of it.

John Machin
A: 

The output from

import sys 
print sys.version 
print sys.path 
print sys.argv[0]

is

2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]
['C:\\Documents and Settings\\david\\My Documents\\GIS_References\\public\\funconn_public', 'C:\\Python25\\Lib\\idlelib', 'C:\\Program Files\\ArcGIS\\bin', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\Python25\\lib\\site-packages\\Pythonwin']
C:\Documents and Settings\david\My Documents\GIS_References\public\funconn_public\polyline2geonetwork.py

Now, when I run the file via IDLE, it compiles without error after I added those lines at the top. However when I run it via ArcGIS with an appropriate input and output (as a script within a toolbox) I still get this error:

<type 'exceptions.ImportError'>: No module named win32api
Failed to execute (polyline2geonetwork2)
womble
As requested, (1) edit your question, so that all the info is in one place (2) show the output from your script AND the traceback when you "run it via ArcGIS", not just the error message.
John Machin
+1  A: 

Your sys.path is

['C:\\Documents and Settings\\david\\My Documents\\GIS_References\\public\\funconn_public', 'C:\\Python25\\Lib\\idlelib', 'C:\\Program Files\\ArcGIS\\bin', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\Python25\\lib\\site-packages\\Pythonwin']

and winapi.py is located in C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp.

Notice that this directory is not listed in your sys.path. To get things working, you'll need to put C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp in your sys.path.

It appears winapi.py is not yet installed. It is in a test\build...\temp directory. I don't know much about Windows+Python. Maybe there is documentation that came with winapi.py which explains how the installation is suppose to be achieved.

A quick (but ugly) fix is to manually insert the needed directory into sys.path. By this I mean, you can edit polyline2geonetwork.py and put

import sys
sys.path.append(r'C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp')

near the top of the file.

unutbu