Phyton script fails to work through Windows 7 Task Scheduler

1023
8
05-14-2012 06:16 AM
CharlesBanks1
New Contributor II
I am having difficulty making a Phyton script work through Windows 7 Task Scheduler. I created a Model and exported the Model to Python and then tried to schedule the script to run on Windows scheduler but I keep getting errors. The Model runs fine but the script fails each time.

The Exported script is below:

# ---------------------------------------------------------------------------
# Centerline_Update_COMMON_SDE.py
# Created on: 2012-05-10 16:34:42.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy


# Local variables:
Centerline_Update_New_shp = "C:\\DRIVE_D_LAST_COMPUTER_DIT51_MJ\\E_911_Data_06_2010\\E_911_Centerline_Update_2012\\Centerline_Update_New.shp"
COMMON_SDE_CENTERLINE_METADATA_xml = "C:\\DRIVE_D_LAST_COMPUTER_DIT51_MJ\\E_911_Data_06_2010\\E_911_Centerline_Update_2012\\COMMON_SDE_CENTERLINE_METADATA.xml"
devGIScommon_CREATOR_COMMON_trn_Centerline = "Database Connections\\TEST_Database Connection to vgissdedev.sde\\devGIScommon.CREATOR_COMMON.Transportation\\devGIScommon.CREATOR_COMMON.trn_Centerline"

# Process: Copy Features
arcpy.CopyFeatures_management(Centerline_Update_New_shp, devGIScommon_CREATOR_COMMON_trn_Centerline, "", "0", "0", "0")

# Process: Metadata Importer
arcpy.MetadataImporter_conversion(COMMON_SDE_CENTERLINE_METADATA_xml, devGIScommon_CREATOR_COMMON_trn_Centerline)



Any help would be greatly appreciated. Thanks,



Charles
Tags (2)
0 Kudos
8 Replies
MichaelVolz
Esteemed Contributor
Charles:

How are you executing this python script in the Windows 7 Task Scheduler?  Are you calling the python script directly or are you calling it in another script such as a bat file?  I was having a similar issue on a Windows 2008 Server.  I had to add PYTHONPATH environmental variable to the directory where the python.exe file resides, in order for python to execute properly without having to specify the full path to the python scripts.

In addition, under Task Scheduler I have set the task to run under highest privileges as this is what many other needed to configure in order for their scripts to run properly.

I hope this information helps.
0 Kudos
CharlesBanks1
New Contributor II
I am executing the .py file directly from the C: folder. The scheduler calls the .py file directly from my hard drive. Are there other tings taht I should be doing?
0 Kudos
MichaelVolz
Esteemed Contributor
Charles:

I execute all my python scripts from a bat file that I assign to the Scheduled Tasker.

Here is a simple example of a bat file that calls 1 python script:

@echo off

echo Start time: %time% > Road_Simplify.log

REM Run the python script
echo ------  Update time: %time%
echo ------  Removing Bezier curves from Roads  --------
echo ------  Removing Bezier curves from Roads  -------- >> Road_Simplify.log

01_Bezier_Remover.py >> Road_Simplify.log


echo End time: %time% >> Road_Simplify.log
@echo on

Please notice that I do not need the full path to the python script as I have PYTHONPATH set as an environmental variable.
0 Kudos
CharlesBanks1
New Contributor II
Thanks again, but I am a bit confused. Are you setting this from Notepad? With where you have : 01_Bezier_Remover.py, being what my Python script should be? Where do you set "PYTHONPATH set as an environmental variable"?
0 Kudos
MichaelVolz
Esteemed Contributor
Charles:

You can open up Notepad and copy my example into the file and name it whatever you want.  I would place it in the same folder as the python script and a log file you can create that would provide information on how the bat process and python script functioned if you have arcpy.print messages written into it.  You would create a new Environmental Variable named PYTHONPATH that would be directed to the directory where python.exe is installed (In my case it is located at c:\Python26\ArcGIS10.0).
0 Kudos
CharlesBanks1
New Contributor II
Sorry to be a bother. But how do I create a new env variable where phyton.exe is located?
0 Kudos
MichaelVolz
Esteemed Contributor
To create an Environmental Variable on Windows 2008 Server (It may be different on Windows 7 especially since it does not sound like it is a Server), you go to the Start menu in the lower left corner.  Right click on Computer in the right side pane and select Properties.  On the left side select Advanced system settings and near the bottom of this page is Environmental Variables.  Click on this button.  Click on New and set the variable as PYTHONPATH and browse to the directory where the Python.exe file is located.  That should do it.
0 Kudos
CharlesBanks1
New Contributor II
Many thanks!!!
0 Kudos