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).
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.
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!
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!
Thanks Joe! To be honest with you, my code was made sort of quick and dirty. I used model builder as a base (exported to python) and then modified and added more code in python using the arcpy modules as necessary. I wrote the code a few years ago. I do have a lot code and 4 scripts to run. Most recently I created a script that runs all 4 of my scripts instead of having to run each of them individually.
My main goal is to have the script run when I'm not in the office. Instead of trying to combine the log-in process with my outdated IDLE 2.7 code I could write a second one in a more current platform. I was thinking of setting up a windows scheduler, I would just set-up 2: one to log-in to AGOL and one to run my code that updates the feature service.
I've used ArcMap for so long that I've been too stubborn to switch over to Pro, GIS is a small part of my job function so I need to be proficient in other non-GIS software as well.
In terms of deprecation, from how I comprehend it, the ArcGIS python libraries were downloaded when I downloaded ArcMap. Even if ESRI no longer supports certain libraries, they exist on my C drive so they should run forever as long as I don't delete them and work with an ArcMap version that supports it (for the code that does the ArcMap stuff). I could be totally wrong here.
Ah ha! I don't know why I've never noticed this but I do see the option to EDIT with IDLE(ArcGISPro)! I'll have to try that out!
Thanks!
I corrected the link.
Amber - Python 2.x is old and in the way. Yes there are some notable differences, and yes some of your scripting will need to be edited. If you have millions of lines of code, the transformation will be painful. Even the versions of Python 3.x that ship with ArcGIS Pro are not current (see Timeline for python upgrade? ). So what to do?
Personally, I only use the IDLE (Integrated Development and Learning Environment) for quick and dirty python scripts, and that's very rare. I prefer a more robust IDE for the heavy lifting and just general script creation. Since you have ArcGIS Pro installed, the default environment will have python 3.x installed. It sounds like you have ArcMap installed as well so in your C drive you have an install directory called Python 2.'something'. (I don't have ArcMap installed anymore). Try this: right click on an existing python script and see if you get the choice to EDIT with IDLE(ArcGISPro). If so, you'll be in the Python 3.x environment and you should be good to go....
edited to add: How sunsetting Python 2 affects ArcGIS
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.
<SPAN class="keyword token">from</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">import</SPAN> os <SPAN class="comment token"># username and password are your ArcGIS Online login credentials</SPAN> your_gis_name <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>username <SPAN class="operator token">=</SPAN> <SPAN class="string token">'your_username'</SPAN><SPAN class="punctuation token">,</SPAN> password <SPAN class="operator token">=</SPAN> <SPAN class="string token">'your_password'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Connected to {} as {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>your_gis_name<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>portalHostname<SPAN class="punctuation token">,</SPAN> your_gis_name<SPAN class="punctuation token">.</SPAN>users<SPAN class="punctuation token">.</SPAN>me<SPAN class="punctuation token">.</SPAN>username<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># the outDir variable is a path to a folder. The folder will contain one or</SPAN> <SPAN class="comment token"># more folders with zipped shapefiles (i.e. <filename>.shp, <filename>.prj, etc.)</SPAN> outDir <SPAN class="operator token">=</SPAN> <SPAN class="string token">'path/to/parent/folder/with/zipped/dirs<S>'</S></SPAN> <SPAN class="comment token"># this will create a list of paths to each zipped feature folder</SPAN> <SPAN class="comment token"># if zipped subdirs appear in folder as: agriculture.zip, forests.zip, commercial.zip</SPAN> <SPAN class="comment token"># scandir orders file paths ALPHABETICALLY! So...</SPAN> <SPAN class="comment token"># zipped_dirs = [path/to/agriculture.zip, path/to/commercial.zip and path/to/forests.zip]</SPAN> zipped_dirs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>subD<SPAN class="punctuation token">.</SPAN>path <SPAN class="keyword token">for</SPAN> subD <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>scandir<SPAN class="punctuation token">(</SPAN>outDir<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Make tags, title, snippet</SPAN> <SPAN class="comment token"># since zipped_dirs will order alphabetically the title, tags and snippets</SPAN> <SPAN class="comment token"># need to be ordered to match zipped dirs as if they were arranged alphabetically</SPAN> title <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'agricultural lands'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'commercial_property'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'forests'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># tags can be a list of lists if multiple tags per item or a simple list</SPAN> <SPAN class="comment token"># with strings if just one tag per item</SPAN> tags <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'land_project'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'agricultural'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'land_project'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'commercial'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'land_project'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'forests'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># snippets</SPAN> snippet <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'some agricultural lands in Kentucky'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'proposed development sites'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'where forests are classified'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># add items to your ArcGIS Online Content</SPAN> <SPAN class="keyword token">for</SPAN> idx<SPAN class="punctuation token">,</SPAN> shp <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>zipped_dirs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> properties_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'title'</SPAN><SPAN class="punctuation token">:</SPAN>title<SPAN class="punctuation token">[</SPAN>idx<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'tags'</SPAN><SPAN class="punctuation token">:</SPAN>tags<SPAN class="punctuation token">[</SPAN>idx<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'snippet'</SPAN><SPAN class="punctuation token">:</SPAN>snippet<SPAN class="punctuation token">[</SPAN>idx<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN> fc_item <SPAN class="operator token">=</SPAN> your_gis_name<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>properties_dict<SPAN class="punctuation token">,</SPAN> data <SPAN class="operator token">=</SPAN> shp<SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
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 .
Hi Randy, the link just brings me back to my post.
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?
See PYTHON - No Module named gis
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.