Set gp.workspace with a Variable Read in from a Text File

434
3
Jump to solution
03-22-2012 06:18 AM
MichaelVolz
Esteemed Contributor
To All Python Users:

In ArcGIS v9.3.1 I want to be able to set certain variables such as the gp.workspace from a configuration file outside the python script so it is not hard-coded in the script.

I was able to read in text for other variables I needed to set but it did not work with gp.workspace as I received the following error:

Traceback (most recent call last):
  File "G:\deptdata\GIS\GIS_Services\User\server_Scripts\01_Data_Read_Config.py", line 37, in <module>
    gp.workspace = "'" + source + "'"
RuntimeError: NotInitialized

The code to set the variable source is as follows:

vars = myfile.read().rstrip().split(",")  connSDE = vars[0] EOC_source = vars[1]  gp.workspace = "'" + EOC_source + "'"  (I have tried gp.workspace = """ + EOC_source + """ as well as gp.workspace = EOC_source 


Can gp.workspace be set with a variable?  If so, what am I doing wrong with the syntax?

Any help or hints regarding this issue are greatly appreciated.  Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Occasional Contributor III
Michael, have you tried using print or arcpy.AddMessage to ensure the variable is coming through as expected? You can also do type(). If you are running as a script tool:
gp.AddMessage(EOC_source) gd.AddMessage(str(type(EOC_source))

or from the command line:
print EOC_source print type(EOC_source)

Either of these should produce (for example):
C:/Datastore/2006/Forestry/
<type 'str'>



EOC_source should be coming through as a string, which means just writing:
gp.workspace = EOC_source

should work.... Although it is possible that the syntax for 9.3 is different.

View solution in original post

0 Kudos
3 Replies
StacyRendall1
Occasional Contributor III
Michael, have you tried using print or arcpy.AddMessage to ensure the variable is coming through as expected? You can also do type(). If you are running as a script tool:
gp.AddMessage(EOC_source) gd.AddMessage(str(type(EOC_source))

or from the command line:
print EOC_source print type(EOC_source)

Either of these should produce (for example):
C:/Datastore/2006/Forestry/
<type 'str'>



EOC_source should be coming through as a string, which means just writing:
gp.workspace = EOC_source

should work.... Although it is possible that the syntax for 9.3 is different.
0 Kudos
StephenBarrow
New Contributor
If you are running this from within an ArcMap/Catalog session you can also create a tool script dialog box and attach your script to it to get the input that way and the dialog will ensure it is a workspace as well.
0 Kudos
MichaelVolz
Esteemed Contributor
Thank you for the reply Stacy.

I was using python syntax that is compatible with ArcGIS v10, but I was mistakenly running the code on an ArcGIS v9.3.1.  Once I ran the code on an ArcGIS v10 machine it worked fine.  This type of stuff happens as I am working in dual environments as my organization works on upgrading GIS software and GIS dependent applications.
0 Kudos