You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - Geoprocessing Scripting (Python, JavaScript, VB) forum

Script runs within Python, but not from too...   Bruce Lang Jan 06, 2005
Re: Script runs within Python, but not from...   Felix Hebeler Jan 09, 2005
Re: Script runs within Python, but not from...   Bruce Lang Jan 10, 2005
Re: Script runs within Python, but not from...   Felix Hebeler Jan 11, 2005
Re: Script runs within Python, but not from...   Bruce Lang Jan 12, 2005
Re: Script runs within Python, but not from...   Felix Hebeler Jan 14, 2005
Re: Script runs within Python, but not from...   Bruce Lang Jan 14, 2005
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Script runs within Python, but not from toolbox 
Author Bruce Lang 
Date Jan 06, 2005 
Message Why would a custom script execute from within pythonwin but not from its toolbox location withon ArcCatalog or ArcMap?

When ran from a toolbox and pressing OK on the parameter dialog (currently with two boolean parameters), it "acts" like it runs with the following message text, but it doesn't run.

--message text start--
Executing (DGN2SHP_Conversion_2): DGN2SHP_Conversion true true
Start Time: Thu Jan 06 11:19:08 2005
Running script DGN2SHP_Conversion...
Executed (DGN2SHP_Conversion_2) successfully.
End Time: Thu Jan 06 11:19:08 2005 (Elapsed Time: 0.00 secs)
--message text end--

If I edit the script and press the pythonwin "run" button the script runs and finishes without error.

I've narrowed my code down to one main statement (show below) and used gp.AddMessage() statements to display the progress (not in code below), but I've not been able to track down the problem.

The same results happen from two PC's setup with ArcGIS 9 SP2, Win XP SP2. One PC has Python 2.4 & PythonWin 2.4, the other still contains the original Python 2.1 and PythonWin 2.1.

Without going into a lot of detail, the Windows registry settings for Python are correct. For ver 2.1 the settings are:

Open = C:\Python21\python.exe "%1" %*
Edit = C:\Python21\Pythonwin\pythonwin.exe /edit "%1"

For ver 2.4 the settings are:

Open = C:\Python24\python.exe "%1" %*
Edit = C:\Python24\Lib\site-packages\pythonwin\Pythonwin.exe /edit "%1"

Thanks.

BTW my script name and location is "M:\GIS\Arc\Scripts and Models\CAD to Shapefile\DGN2SHP.py"
 
 
# ---------------------------------------------------------------------------
# DGN2SHP.py
# Created on: Mon Jan 03 2005 10:46:27 AM
# Description: Further automates the process of converting "county-wide"
#              Microstation design (DGN) files to Shapefiles (SHP)
#
#              Some files require further processing like Parcels and
#              ParcelBLKNum (see details later in this script.)
# ---------------------------------------------------------------------------
# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Obtain User Parameters
do_conversion = sys.argv[1]
do_fieldedit  = sys.argv[2]

# Local variables...
DIR_Base = "C:/TEMP/Conversion/"
DIR_DGN  = DIR_Base + "DGN/"
DIR_SHP  = DIR_Base + "SHP/"

Staging_mdb = DIR_Base + "Staging.mdb"

DGN_Polyline     = ["CorporateLine", "CountyLine", "GeoTWPLine", "LotLine","ParcelLine",\
					"PoliticalTWPLine", "RailroadCenterline", "RailroadROW", "RoadCenterLine",\
					"RoadROW", "SectionLine", "SubLine", "WaterLine"]
DGN_Annotation   = ["LotDimension", "LotNumber", "ParcelAcreage", "ParcelDimension", "ParcelNumber",\
					"Railroadname", "RoadName", "SectionNumber", "SubBlkNum", "SubName", "WaterName"]
DGN_Parcels      = "Parcels"        # requires manual finishing - see notes later in this script
DGN_ParcelBLKNum = "ParcelBLKNum"   # requires manual finishing - see notes later in this script
ParcelBLKNum_ImportCAD_mdb = DIR_Base + "ParcelBLKNum_ImportCAD.mdb"    # temp geodatabase


# ---------------------------
# CONVERSION
# ---------------------------
gp.AddMessage("Processing Conversions...")

if do_conversion == "true":

	# Assign projection...

	ProjString = "PROJCS['NAD_1983_UTM_Zone_15N',GEOGCS['GCS_North_American_1983'," \
				 "DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]]," \
				 "PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator']," \
				 "PARAMETER['False_Easting',1640416.666666667],PARAMETER['False_Northing',0.0]," \
				 "PARAMETER['Central_Meridian',-93.0],PARAMETER['Scale_Factor',0.9996]," \
				 "PARAMETER['Latitude_Of_Origin',0.0],UNIT['Foot_US',0.3048006096012192]]"
	
	SourceFeature = DIR_DGN + DGN_ParcelBLKNum + ".dgn"
	try:
		gp.ImportCAD_conversion(SourceFeature, ParcelBLKNum_ImportCAD_mdb, ProjString, "Explode_Complex")
	except:
		gp.AddWarning("Unable to create '" + ParcelBLKNum_ImportCAD_mdb + "' staging file for: " + SourceFeature)

	gp.AddMessage("Finished.")
else:
	gp.AddMessage("Skipped.")
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Felix Hebeler 
Date Jan 09, 2005 
Message Try using
gp.GetParameterAsText(0)
instead of
sys.argv[1]
The first parameter is accessed using index 0, not 1 as with sys.argv, though!

I am afraid the script won't run as a standalone tool anymore than, but it works fine within ArcGIS.
I have two versions of my tools now, one for the toolbox, one as a standalone for the commandline.
 
 
>>>> Instead of 

# Obtain User Parameters
do_conversion = sys.argv[1]
do_fieldedit  = sys.argv[2]

>>>> use

# Obtain User Parameters
do_conversion = gp.GetParameterAsText(0)
do_fieldedit  = gp.GetParameterAsText(1)
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Bruce Lang 
Date Jan 10, 2005 
Message Thanks Felix, but I've tried that too. In fact, the following script bombs, that is exits without displaying an error at the first "do_conversion = gp.GetParameterAsText(0)" line.

When ran from a toolbox with two boolean parameters the script window displays:

Executing (Script_1): Script true true
Start Time: Mon Jan 10 10:54:35 2005
Running script Script...
Executed (Script_1) successfully.
End Time: Mon Jan 10 10:54:36 2005 (Elapsed Time: 1.00 secs)

But, when ran through pythonwin 2.1 or 2.4 python simply exits without an error. When I run the script through the pythonwin debugger it also bombs out.

Maybe I'm missing a "load" value or something? The scripts I've created so far were exported as a python script from modelbuilder diagram and combined into a larger script.

Both of my PC's are; Windows XP Pro SP2, ArcGIS 9.0 SP2 (arcview level)

One PC uses the version of python that came with ArcGIS 9.0, that is 2.1, and the other PC was updated to python/pythonwin 2.4. The update was done in an attempt to fix problems I was having running python 2.1 scripts.
 
 
# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Obtain User Parameters
do_conversion = gp.GetParameterAsText(0)
do_fieldedit  = gp.GetParameterAsText(1)

gp.AddMessage("do_conversion = " + do_conversion)
gp.AddMessage("do_fieldedit = " + do_fieldedit)
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Felix Hebeler 
Date Jan 11, 2005 
Message Hi Bruce.

Ok, its been a pain, but I got it working here (at least theoretically, as I do not have your data).
I reproduced the bailing out. Very strange behaviour, as it ran fine for once and than produced errors...

Here is what I did:

-Use the gp.GetParameterAsText(0) option.
Make sure you set the parameters in the tool dialog correct:
data type: boolean (creates a checkbox in the dialog), give a default false/true, no parentheses!
-I used the gp.overwriteoutput = 1 option
-I also re-set do_conversions='false' at the beginning of the script
-Check the script your working and editing is REALLY the one that the toolbox points to (tool dialog, tab 'source').
Cost me half an hour once... ;-)
-Be sure you don't have a compiled version of your script in the directory (DGN2SHP.pyc),
especially if you are using defaults (like my do_conversions='false' :-/ )
Check regularly and delete the pyc files. This can happen when you debug for example,
check the settings for when to create combiled versions of your scripts.
- I found scripts bail out in arcgis sometimes (!??!) when you give two parameters in the tool dialog but only ask for one in the script.
Quite rediculous...
- I obviously changed some path info, replace them in my code below.
- I kicked out the line-continuers '\' in your strings...
- Restart ArcGIS and Pythonwin frequently - hey, we're working with windows here! it really solves a problem too often...

All these things sound pretty obscure, but I gave up on understanding WHY arcgis and python do strange things. if you ask me, I don't blame python though...
sometimes it appears values are not flushed between tool runs which is quite a pain.
Honestly, it took me 2 hours to get the script working, it was REALLY strange, I wonder if there is something in the gp.ImportCAD_conversion that triggers this.
now this is not the most sophisticated help I know, but it works for me now and I can't get it to crash anymore.
so I just hope this will work for you.
my settings are all the same, except that i use python 2.3.4 
 
# Import system modules
import sys, string, os
import win32com.client
arcToolboxPath = "C:/ESRI/ArcGis/ArcToolbox/Toolboxes/"  # Path to ArcGIS Toolboxes (set to default)
arcTemp = "D:/ArcWorkSpace/Scratch/"    # Path for temporary files
# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.overwriteoutput = 1
# Load required toolboxes...
gp.AddToolbox(arcToolboxPath+"Conversion Tools.tbx")
gp.AddToolbox(arcToolboxPath+"Data Management Tools.tbx")

cflag = 'false'
eflag = 'false'
# Obtain User Parameters
cflag = gp.GetParameterAsText(0)
eflag = gp.GetParameterAsText(1)

# Local variables...
DIR_Base = arcTemp+"DGN2SHP"
DIR_DGN  = DIR_Base + "DGN/"
DIR_SHP  = DIR_Base + "SHP/"

Staging_mdb = DIR_Base + "Staging.mdb"

DGN_Polyline     = ["CorporateLine", "CountyLine", "GeoTWPLine", "LotLine","ParcelLine","PoliticalTWPLine", "RailroadCenterline", "RailroadROW", "RoadCenterLine","RoadROW", "SectionLine", "SubLine", "WaterLine"]
DGN_Annotation   = ["LotDimension", "LotNumber", "ParcelAcreage", "ParcelDimension", "ParcelNumber","Railroadname", "RoadName", "SectionNumber", "SubBlkNum", "SubName", "WaterName"]
DGN_Parcels      = "Parcels"        # requires manual finishing - see notes later in this script
DGN_ParcelBLKNum = "ParcelBLKNum"   # requires manual finishing - see notes later in this script
ParcelBLKNum_ImportCAD_mdb = DIR_Base + "ParcelBLKNum_ImportCAD.mdb"    # temp geodatabase


# ---------------------------
# CONVERSION
# ---------------------------
gp.AddMessage("Processing Conversions...")

if flag == "true":

    # Assign projection...

    ProjString = "PROJCS['South_America_Albers_Equal_Area_Conic',GEOGCS['GCS_South_American_1969',DATUM['D_South_American_1969',SPHEROID['GRS_1967_Truncated',6378160.0,298.25]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-60.0],PARAMETER['Standard_Parallel_1',-5.0],PARAMETER['Standard_Parallel_2',-42.0],PARAMETER['Latitude_Of_Origin',-32.0],UNIT['Meter',1.0]]"
    
    SourceFeature = DIR_DGN + DGN_ParcelBLKNum + ".dgn"
        gp.ImportCAD_conversion(SourceFeature, ParcelBLKNum_ImportCAD_mdb, ProjString, "Explode_Complex")
    except:
        gp.AddWarning("Unable to create '" + ParcelBLKNum_ImportCAD_mdb + "' staging file for: " + SourceFeature)
        print "Unable to create '" + ParcelBLKNum_ImportCAD_mdb + "' staging file for: " + SourceFeature
        sys.exit()

    gp.AddMessage("Finished.")
else:
    gp.AddMessage("Skipped.")
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Bruce Lang 
Date Jan 12, 2005 
Message Felix,

Thank you for taking the time to look this problem. In a nut shell - running custom scripts is, even on a good day, terrible. Okay, I've yet to read ESRI's documentation on Python scripts, but why does the system fail even on extreemly simple scripts. (see my "simple" script in this thread)

The suggestions you made did not fix the problem.

-I'm now using gp.GetParameterAsText() and verified the dialog parameters
-Used the gp.overwriteoutput = 1 option
-Set the parameters to "false" prior to getting them
-Verified the toolbox is pointing to my script
-There are no compiled DGN2SHP files

Gotta go, but I'll report back anything else I find out. Once again - thanks for your extended help in this matter. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Felix Hebeler 
Date Jan 14, 2005 
Message Hm.
I had a script that basically only read in one parameter using gp.getparameterastext() and then printed it on the screen using gp.addmessage().
and it failed.
restarted arcgis, it ran.
tried to run it again, failed.
did all the fuss I posted up there and eventually it worked.
it's so tedious...
but on the other hand:
we can be sure the code is correct. it works for me.
so it's got to be a configuration issue.
hey, we're almost there... *sigh*
any temporary files you can delete?
what extensions are loaded?
I experienced problems with custom tools and extensions loaded.
try a clean start loading only the absolutely necessary extensions.
are you working on a network drive? Try copying everything on a local disk.
did you try reinstalling arcgis and python?
(my tips are getting worse, I know)
Sorry, can't think of anything better now.
I'll keep it in my mind though.
Please post any solution you find, and good luck!
Cheers
Felix 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Script runs within Python, but not from toolbox 
Author Bruce Lang 
Date Jan 14, 2005 
Message Temporary files - tried deleting all files in "%USERPROFILE%\Local Settings\Temp" and other places, but no change.

Extensions - Publisher is the only extension and it does not matter if this extension is enabled or disabled.

Network - The only networked file I was working with is the script file. I copied all working files to my local drive and was processing them locally. Using a local copy of the script did not reslve any issues.

Uninstall/Reinstall ArcGIS & Python & Other supporting files and configurations - Tried too many times in the past without resolving ANY issues (what a pain!!) so why try now? ESRI should have a utility to verify/resolve installation issues without requiring a remove and reinstall. (oh, did I mention - what a pain!!)

Thanks Felix - Take care