Import arcpy and Windows Task Scheduler

1385
9
Jump to solution
08-18-2018 08:58 AM
RobertKnapik
New Contributor III

I have problem with Task Scheduler in Windows Server 2012. I would like to run script in Task Scheduler in which I use arcpy. Unfortunately when I use arcpy in my script Task Scheduler does not work correctly.

I have following test script which print current Python version: 

 

   import platform
   import os
   import sys
   import datetime
   import arcpy

   filename = os.path.join(os.path.dirname(sys.argv[0]), 'plik.txt') #zapis do folderu ze skryptem
   fid = open(filename, 'a')
   fid.write("\nhello\t")
   fid.write(platform.python_version())
   fid.close()

When I delete line with "import arcpy" everything is ok. 

Does anyone know how to solve this problem?

1 Solution

Accepted Solutions
RobertKnapik
New Contributor III

I solved the problem. I repaired the ArcGIS Pro installation and everything is ok.

View solution in original post

0 Kudos
9 Replies
simoxu
by MVP Regular Contributor
MVP Regular Contributor

According to what you described about problem, it's likely the failure of loading the arcpy module is the cause. I would suspect that you have multiple python interpreters installed on your machine, and the one you are using to run the script does not have arcpy in its system path.

You can print out the system path and visually check it.

import sys
from pprint import pprint

pprint(sys.path)

If this is the case, you can add the path of your arcpy module in the PYTHONPATH environment variables settings. Or, you simply append or insert the paths in your script to make it an application level setting.

sys.path.append('<Pro home folder>\\bin\\Python\\envs\\arcgispro-py3')
sys.path.append('<Pro home folder>\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages")
‍‍
0 Kudos
RobertKnapik
New Contributor III

I can import arcpy in Python Command Prompt and Python 3.6.5. 

I used Python Command Prompt and Python 3.6.5. and something like this was printed:

Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bi
t (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint
>>> print (sys.path)

'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python36.zip',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\DLLs',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages',
'E:\\Program Files\\ArcGIS\\Pro\\bin',
'E:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy',
'E:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcToolbox\\Scripts',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\future-0.16.0-py3.6.egg',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\pytz-2018.3-py3.6.egg',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\win32',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\win32\\lib',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\Pythonwin',
'E:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\sympy-1.1.1-py3.6.egg'

I can import arcpy in Python (Command line)  and Python 2.7.14. I used Python (Command line) and Python 2.7.14 and somtehing like this was printed:

Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32.

>>> import sys
>>> from pprint import pprint
>>> print (sys.path)

'E:\\Program Files (x86)\\PyScripter\\Lib\\rpyc.zip',

'C:\\Windows\\SYSTEM32\\python27.zip',

'E:\\Python27\\ArcGIS10.6\\DLLs',

'E:\\Python27\\ArcGIS10.6\\lib',

'E:\\Python27\\ArcGIS10.6\\lib\\plat-win',

'E:\\Python27\\ArcGIS10.6\\lib\\lib-tk',

'E:\\Python27\\ArcGIS10.6',

'E:\\Python27\\ArcGIS10.6\\lib\\site-packages',

'E:\\Program Files (x86)\\ArcGIS\\Desktop10.6\\bin',

'E:\\Program Files (x86)\\ArcGIS\\Desktop10.6\\ArcPy',

'E:\\Program Files (x86)\\ArcGIS\\Desktop10.6\\ArcToolBox\\Scripts'

I would like to run my script in Task Scheduler in Python 3.6.5 because in this script I use arcpy and ArcGIS API for Python.

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Not sure what else can cause the problem. can you share the settings in your task scheduler?

A quick search gave me this link, hope it helps. 

Scheduling a Python script or model to run at a prescribed time 

0 Kudos
RobertKnapik
New Contributor III

It seems to me that settings in my Task Scheduler are rather ok. I tried everything. 

0 Kudos
RobertKnapik
New Contributor III

I tried again and I noticed that the problem is only then I use python 3.6.5.

In following examples Task Scheduler does not work corectly unless I remove line "import arcpy" from script (test.py):

I also tried to use propy.bat, but the result is the same.

When I use following settings Python 2.7.14 runs and Task Scheduler works correctly even with "import arcpy" in script:

(but I have to use Python 3.6.5 because I use also ArcGIS API for Python in my script )

0 Kudos
MichaelVolz
Esteemed Contributor

What version of Pro do you have installed on this machine?

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Please be aware that arcpy is licensed.

the script needs to be authorized to run outside of Pro.

See the Authorizing Python outside the application part in the following document:

 Python in ArcGIS Pro—ArcPy Get Started | ArcGIS Desktop 

MichaelVolz
Esteemed Contributor

Does this authorization requirement exist for both Named User and Concurrent Use licensing in Pro?

0 Kudos
RobertKnapik
New Contributor III

I solved the problem. I repaired the ArcGIS Pro installation and everything is ok.

0 Kudos