Run Python API script as scheduled task?

9792
35
Jump to solution
04-18-2018 01:38 PM
ShawnHibbs1
New Contributor II

I have created a script that updates a feature service through the Python API in Jupyter Notebooks. I was wondering how I can run this as a scheduled task? 

35 Replies
by Anonymous User
Not applicable

The ArcGIS API for Python can be used in both interactive Jupyter notebooks or in standalone Python scripts. Do you run into issues when using the API within a standalone script?

by Anonymous User
Not applicable

Yes, I have been unable to run it in a standalone script. I have the API installed through ArcGIS Pro but the standalone script returns an error when I try to import arcgis. Are there additional steps I need to take to get it installed?

0 Kudos
by Anonymous User
Not applicable

What error does it show? I'm guessing "Unable to import module 'arcgis'" or something similar.

How are you running the script? Are you providing the path to the correct environment/interpreter?

by Anonymous User
Not applicable

When I export the script to a .py from my .ipynb this is what I get.....

# coding: utf-8

# In[1]:


import arcgis, datetime, os
d = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d")

username = "*************"
password = "**********"
GIS = arcgis.gis.GIS("https://arcgis.com", username=username, password=password)


# In[2]:


item_ids = [
    "***************"
            ]

export_AGO_folder = "Exports"
export_folder = r"***************************"
export_format = "File Geodatabase" #options Shapefile, CSV, File Geodatabase, 
                                   #Feature Collection, GEoJSON, Scene Package, KML
delete_tmp_files_after_export = True


# In[3]:


for item in item_ids:
    print("------------------\nProcessing item: {}".format(item))
    item_object = arcgis.gis.Item(GIS, item)
    if item_object.type != "Feature Service":
        print("Item {} is not a feature service, skipping".format(item))
        continue
    export_name = d + "_export_" + item_object.title
    result_item = item_object.export(export_name, export_format, wait=True)
    print("Exported: {}".format(result_item))
    if export_AGO_folder:
        result_item.move(export_AGO_folder)
    download_result = result_item.download(export_folder)
    print("saved to: {}".format(download_result))
    if delete_tmp_files_after_export:
        result_item.delete()
        print("Deleted result")

I tried to run this as a stand alone and receive this error....

0 Kudos
by Anonymous User
Not applicable

How are you running the script?

Are you providing the correct Python interpreter. Can you try providing the full path the arcgispro-py3 interpreter. It should be similar to the following.

C:\>"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcgis
>>>

by Anonymous User
Not applicable

0 Kudos
by Anonymous User
Not applicable

The IDE you have open right now appears to be using Python 2.7.14. You need to switch your interpreter to use the Python interpreter that ships with ArcGIS Pro which should be Python 3.6.5 (if pro 2.2 I think).

Try this:

1. Open command prompt

2. Type "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" and hit enter. This should launch the Python Interpreter that Pro uses. Also please check that the above path is correct for you machine.

3. Type "import arcgis" and hit enter. This should import the arcgis module

If the above works then you should be able to run the following from command prompt:

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" "<your script file.py>"

This should run your script using the ArcGIS Pro Python Interpreter.

by Anonymous User
Not applicable

0 Kudos
by Anonymous User
Not applicable

Oh sorry, if I wasn't clear. The steps 2 and 3 were just to test the Python API import correctly. 

To run your script just do:

1. Open command prompt

2. Type "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" "<your script file.py>" and hit enter