Task Scheduler Python Issue

Good Morning All,

We have a python script that is putting SQL spatial data into a shapefile, compressing it, and uploading it to AGO for a hosted feature service update. The script runs with no issue in python itself, however when we run it in task scheduler, the second step is to create the zip file and it just hangs there.

We have tried changing variables to all forms of absolute paths with no change. Has anyone had any experience with this or has any thoughts, here is the portion that it hangs on:

Thanks in advance!

#2 Create Zip File

zip_path = r"\\vm-fil2\GIS\Tools\Python\PD Public Incidents\PDIncident.zip"
directory_to_zip = r"\\vm-fil2\GIS\Tools\Python\PD Public Incidents\Shapefile"

folder = pathlib.Path(directory_to_zip)

with ZipFile(zip_path, 'w', ZIP_DEFLATED) as zip:
   for file in folder.iterdir():
        zip.write(file, arcname=file.name)


Can you elaborate on what running “in python itself” looks like? Are you running the script with the same environment that the Task Scheduler uses? Oh, and are we talking the task scheduler in ArcGIS Pro, or Windows?

Windows Task Scheduler

It’s been a while since we were scheduling Python tasks through Windows, but I seem to recall needing to use a .bat script that called the appropriate conda env, then run the file from there. But the zipfile stuff is built-in, really shouldn’t matter what env is running.

Task Scheduler also has the setting for “start in”, defining what folder the script should run in. An absolute path should avoid any issues there, but I could see ways the execution path might still matter.

How is the task running? Can you elevate its privileges? Maybe the task scheduler is executing in a way that does not have appropriate permissions to create the zip file?

An Alternate Approach?

We have a lot of layers that update online, but originate as a SQL query. We use a process that looks like:

Query SQL → Apply as edits to Feature Service

Avoids any intermediate file / upload / overwrite. Might be worth a shot if your task is still having issues with the zip file.

Thanks for the response Josh.

Sorry for the confusion, eys running in Python meant running it from IDLE in the directory provided to Task Scheduler. I agree that the .bat should not really help the zip process. I do have the start in filled in the with the correct path as well.

The task is running through my admin account with the option for do not save password unchecked and the run with the highest priveleges option checked. I’m assuming that it is a permission issue with task scheduler still but I’m not certain what else can be done.

That said, I think we are going to have to look into other ways of accomplishing this without exporting the SQL spatial view into a shapefile.

So, when something is running in IDLE, what Python executable is it using? The same as the task scheduler is looking for? It’s entirely possible to have multiple instances of Python on your machine, especially if you have any GIS software installed. You’ll want to make sure that when you say “python”, it’s the same thing the task is saying when it says “python”.

Without drastically changing your script, I would highly suggest using conda to create a python environment for your scripting, then schedule a task like

/path/to/conda/envs/your-env/bin/python.exe your-python-script.py

That would ensure that there aren’t any local configurations that differ between you and the task itself, since it would explicitly call that particular environment.

Thanks Josh, I’ll add that to the script, we did figure it out. It was a path permission issue.

We are using the same python path to the argispro-py3 folder that we were running in Idle manually