I am following the exercise on chapter, Python Primer, to run scripts in the command window. When I do the second exercise, I get a message:
C:\PYTHON_PRIMER\Chapter11>python CommandLine_Clip.py 'c:\PYTHON_PRIMER\Chapter1
1\Data' City_Facilities.shp Central_City_CommPlan.shp c:\PYTHON_PRIMER\Chapter11
\MyData' out_file.shp
Traceback (most recent call last):
File "CommandLine_Clip.py", line 15, in <module>
import arcpy, sys, os, traceback, datetime
ImportError: No module named arcpy
However I have no problem running the arcpy command in PyScripter or the Python Window, just in the command prompt.
The computer system variable path is pointing to python:
........................;C:\Program Files (x86)\ArcGIS\EsriProductionMapping\Desktop10.1\Bin;C:\Program Files (x86)\ArcGIS\Desktop10.1\Bin;c:\Python27\ArcGIS10.1
What could be the problem? Thanks!
Here is the script
C:\PYTHON_PRIMER\Chapter11>python CommandLine_Clip.py 'c:\PYTHON_PRIMER\Chapter1
1\Data' City_Facilities.shp Central_City_CommPlan.shp c:\PYTHON_PRIMER\Chapter11
\MyData' out_file.shp
Traceback (most recent call last):
File "CommandLine_Clip.py", line 15, in <module>
import arcpy, sys, os, traceback, datetime
ImportError: No module named arcpy
However I have no problem running the arcpy command in PyScripter or the Python Window, just in the command prompt.
The computer system variable path is pointing to python:
........................;C:\Program Files (x86)\ArcGIS\EsriProductionMapping\Desktop10.1\Bin;C:\Program Files (x86)\ArcGIS\Desktop10.1\Bin;c:\Python27\ArcGIS10.1
What could be the problem? Thanks!
Here is the script
Code:
# Batch and Schedule a Geoprocessing Script
# Created by: Nathan Jennings
# www.jenningsplanet.com
# Created on: 05.24.2011
# Update on: 10.24.2011
# Copyright: 2011
'''
!!!! Change the output and log file paths as needed where the Chapter 11 folders exist.!!!
Save these changes. Test the script to make sure it executes properly.
'''
import arcpy, sys, os, traceback, datetime
CURDATE = datetime.date.today()
try:
# workspace for input data
# e.g c:\\pythonprimer\\chapter11\\data
arcpy.env.workspace = arcpy.GetParameterAsText(0)
# name of input shapefile feature class to be clipped
# i.e. city_facilities.shp
infile = arcpy.GetParameterAsText(1)
# name of clip shapefile feature class to use as the clip file
# i.e. central_city_commplan.shp
clipfile = arcpy.GetParameterAsText(2)
# output folder name for output
# e.g. c:\\pythonprimer\\chapter11\\mydata
output_ws = arcpy.GetParameterAsText(3)
# name of the output feature class
# NOTE: the output path is hard coded to the following path
# the user will need to change this location
outfile = output_ws + os.sep + arcpy.GetParameterAsText(4)
# location of log file for tracking messages
logfile = output_ws + '\\log' + str(CURDATE) + '.txt'
if arcpy.Exists(logfile):
arcpy.Delete_management(logfile)
# Open the log file for writing
log = open(logfile, 'a')
print '\nRunning Clip Routine...'
print >> log, 'Running Clip Routine...'
print 'Input workspace is: ' + arcpy.env.workspace
print >> log, 'Input workspace is: ' + arcpy.env.workspace
print 'Input file is: ' + infile
print >> log, 'Input file is: ' + infile
print 'Clip file is: ' + clipfile
print >> log, 'Clip file is: ' + clipfile
print 'Output workspace is: ' + output_ws
print >> log, 'Output workspace is: ' + output_ws
print 'Output file is: ' + outfile
print >> log, 'Output file is: ' + outfile
if arcpy.Exists(outfile):
arcpy.Delete_management(outfile)
arcpy.Clip_analysis(infile, clipfile, outfile)
print 'Completed Clip'
print >> log, 'Completed Clip'
log.close()
except:
print arcpy.GetMessages(2)
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "arcpy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
print msgs
print pymsg
arcpy.AddMessage(arcpy.GetMessages(1))
print arcpy.GetMessages(1)