How to Use Python to Log into AGOL

1112
11
08-25-2020 10:58 AM
AmberTobin
New Contributor III

We have an AGOL Organization account. Every day I run python code in the IDLE (2.7.16) window that automates stuff in ArcMap and uploads it to AGOL. The only problem is that I have to manually go into ArcMap to sign into AGOL. Is there code I can add to my script that will log me in? I've looked into the ArcGIS API for Python and its all a little over my head. I can't get the GIS module to import into IDLE (2.7.16). 

0 Kudos
11 Replies
JoeBorgione
MVP Esteemed Contributor

See PYTHON - No Module named gis 

That should just about do it....
0 Kudos
AmberTobin
New Contributor III

Hi Joe,

Thanks for the link, I was afraid the gis module doesn't work with Python 2.x but it does with Python 3. I have Pro installed, ESRI doesn't recommend downloading python versions that do not come pre-installed with the ArcGIS software. Any idea where I would find version 3 IDLE on my computer and not be Jupyter? Also, from what I read the coding is slightly different for Python 3. I have a lot of code that I don't plan on rewriting .

0 Kudos
RandyBurton
MVP Regular Contributor

If you wish to use IDLE (2.7.16), there is an old script here: how to get token to access a map service or feature service?

0 Kudos
AmberTobin
New Contributor III

Hi Randy, the link just brings me back to my post.

0 Kudos
RandyBurton
MVP Regular Contributor

I corrected the link.

ZacharyUhlmann
New Contributor III

Hi Amber,

I've recently been on the same path and have developed / scrounged / assembled some functionality for adding data to an ArcGIS Online Group I own.  I will provide some Python scripts, BUT you need Python 3 to use the the Python ArcGIS API.  So the code to get you going is minimal, but getting a Python 3 environment may be where the heavy mental lifting and possible exacerbation comes in.  However, I am no computer genius, I simply installed Arc Pro which will install Python 3.  I run my scripts through the command line in Conda environments.  If you are not familiar with the Command Line, I can respond with some instructions on getting started there.  Here is some code you can save in a script and run(<script_name>.py  i.e.  add_to_agol.py) or run from Arc Pro Python console (that exists right?).  Line 5 is where you access your AGOL account, line 37 adds your zipped shapefiles.

from ArcGIS.gis import GIS
import os

# username and password are your ArcGIS Online login credentials
your_gis_name = GIS(username = 'your_username', password = 'your_password')
print('Connected to {} as {}'.format(your_gis_name.properties.portalHostname, 
                                    your_gis_name.users.me.username))

# the outDir variable is a path to a folder.  The folder will contain one or
# more folders with zipped shapefiles (i.e. <filename>.shp, <filename>.prj, etc.)
outDir = 'path/to/parent/folder/with/zipped/dirs'

# this will create a list of paths to each zipped feature folder
# if zipped subdirs appear in folder as: agriculture.zip, forests.zip, commercial.zip
# scandir orders file paths ALPHABETICALLY! So...
# zipped_dirs = [path/to/agriculture.zip, path/to/commercial.zip and path/to/forests.zip]
zipped_dirs = [subD.path for subD in os.scandir(outDir)]

# Make tags, title, snippet
# since zipped_dirs will order alphabetically the title, tags and snippets
# need to be ordered to match zipped dirs as if they were arranged alphabetically
title = ['agricultural lands', 'commercial_property', 'forests']

# tags can be a list of lists if multiple tags per item or a simple list
# with strings if just one tag per item
tags = [['land_project', 'agricultural'],['land_project', 'commercial'],
        'land_project', 'forests']
# snippets
snippet = ['some agricultural lands in Kentucky', 'proposed development sites',
            'where forests are classified']

# add items to your ArcGIS Online Content
for idx, shp in enumerate(zipped_dirs):
    properties_dict = {'title':title[idx],
                        'tags':tags[idx],
                        'snippet':snippet[idx]}
    fc_item = your_gis_name.content.add(properties_dict, data = shp)

Here is a link for setting up Python 3 through the Command Line

Python and ArcGIS Pro 2.2 

That blog also mentions that many people just use Arc Pro, similar to the Python Console in ArcMap I imagine.

Dan Patterson has dozens of useful blogs on setting up Python, Pro, Conda, etc.  Dan Patterson .  Here is one that has multiple links to traverse /blogs/dan_patterson/2018/07/01/arcgis-pro-your-conda-environments .  * Note he recently set up a new username if you need future content *

And here is the ArcGIS Python API tutorials from which I gleaned those two lines of code using the GIS module Using the GIS | ArcGIS for Developers 

Good luck!  Let me know what you find out and where you get hung up...

Zach

AmberTobin
New Contributor III

Thanks Zach! My main goal is to have the code run when I'm scheduled off from work. I'm thinking I can have another code that just logs into AGOL. I've looked at the API library before I created this post. It seemed so daunting! I tried to run the gis modules in python 2.7 and then realized that won't work after seeing other posts. As Joe mentioned, I do have python version 3.0 so I can run i can test to see if the gis modules work there. Thanks and good luck in your GIS endeavors!

0 Kudos
ZacharyUhlmann
New Contributor III

Of course and...whoops!  I got a little carried away and projected my own issues into your question.  Basically I answered a completely different question.  But those two lines from the Arcgis Python API can be used to log you in programmatically.  Sounds like an interesting endeavor scheduling your code to run independently.  I know the Airflow software (Scheduler — Airflow Documentation ) is popular for scheduling - it's Python and Open Source.  No idea its compatibility with Arc stuff.  But honestly the ArcGIS API is not that hard - don't be intimidated!  

0 Kudos
AmberTobin
New Contributor III

Lol, No worries! I found this the other day and thought this should help me in my automation process: Schedule a Python script or model to run at a prescribed time: 2019 update. Windows has a task scheduler built-in, I have it set to run tomorrow morning so we'll see what happens. Our ITS department isn't too keen on us downloading random software so I try to work with what I have first.

0 Kudos