This code original worked when run through the python command window in ArcCatalog. After I added the script to a toolbox I have had all sorts of troubles.
The most frustrating error has been:
NameError: name 'projectGDB' is not defined
Here is a section of the code. Unfortunately, the error persists. Again, the error only occurs when run as a tool from a toolbox. The NameError does not occur when copied and pasted into the command window.
# Import arcpy module & other helpful
import arcpy, os, xlwt
# Name Project
projectName = arcpy.GetParameterAsText(0)
if projectName == "#" or not projectName:
projectName = "TEST"
# Create Project Folder
projPath = arcpy.GetParameterAsText(1)
if projPath == "#" or not projPath:
projPath = "F:\\GIS\\TEST"
arcpy.AddMessage("Folder path for project is " + projPath)
print "Folder path for project is " + projPath
# Create Project Geodatabase
try:
arcpy.CreateFileGDB_management(path, projectName, "CURRENT")
except Exception as e:
print('Error creating gdb {0} at {1}'.format(projectName, projPath))
print('Exception: {0}'.format(e.message))
# Set Work Environment
projectGDB = os.path.join(path, projectName + ".gdb")
arcpy.env.workspace = projectGDB
arcpy.env.scratchWorkspace = projectGDB
arcpy.env.overwriteOutput = True
arcpy.AddMessage("Workspace is set to " + projectGDB)
#Where is Landcover.gdb located?
landcoverGDB = arcpy.GetParameterAsText(2)
if path == "#" or not landcoverGDB:
landcoverGDB = "F:\\GIS\\TEST\\Landcover.gdb"
# Load NRI Datasets
building = landcoverGDB + "\\imperv_building"
#[script continues for another 200 lines]
Traceback (most recent call last): File
"F:\GIS\Packaged\NRI_sumAcres.py", line 23, in
print "Project geodatabase is " + projectGDB NameError: name 'projectGDB' is not defined
Generally speaking, I do not understand why the script ran without error when copied and pasted into the python window of ArcCatalog, but now is dysfunctional once added to a toolbox.
Best Answer
Don't manually concatenate paths. It leads to errors and makes your code non-portable to other OS's. Use os.path.join instead. For example, replace any instance of
with :
I believe this will solve your problem, since "F:\GIS\TEST" doesn't end in a path separator, you're just sticking the projectName.gdb onto the end of it.
If that doesn't solve it, the line number where python is reporting the exception would help, as would sample output.