Script works on one PC but not 2nd PC

1422
14
05-13-2022 12:43 AM
LindsayRaabe_FPCWA
Occasional Contributor III

Hi brains trust. I need a bit of support here. I have a python script that accesses an ArcOnline feature service and downloads the data, saving it as a feature class in a local (network) GDB. I know the script works as I have it running on one PC, but on another, despite running it in the same software (IDLE for ArcGIS Pro) it fails. The error is that the parameters are not valid and the input dataset does not exist or is not supported. This is despite the same login credentials for ArcOnline being used. I'm at a loss as to why it won't work. It works fine for all my other scripts which are point/line/polygon feature services. The only difference is this one is a related table for a line feature service. The image below shows the same script and the results (left screen is the failure on the remote PC - right screen is on my PC). 

LindsayRaabe_FPCWA_0-1652427702546.png

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
14 Replies
DonMorrison1
Occasional Contributor III

Hi Linsday  - it's really hard to read your code when it is posted that way - this article describes a better way. I would start by dumping out all of the paths that are referenced on the failing PC an simply see if I can access them from a DOS Command window.  In particular I would look at the paths in the related tables properties (et. the origin and destinations). Another idea is to try it from a 3rd PC and see it it works there.

LindsayRaabe_FPCWA
Occasional Contributor III

Sorry for the dodgy code sample - I normally do include a proper code sample instead of just screenshots. I suppose I was just trying to demonstrate that both were the same but showed different outcomes. Will make sure I include the code sample as well next time! I've also included a code sample above. 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
DanPatterson
MVP Esteemed Contributor

The reincarnated formatting version

Code formatting ... the Community Version - Esri Community


... sort of retired...
AlfredBaldenweck
MVP Regular Contributor

Hi Lindsay,

Is your DestGDB located on that remote computer at all? If so, is it on a network drive with a different name?

 

0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

I've included the sample code above now. The output GDB is on a network folder (directly mapped - not using a drive letter).

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
PDodd
by
New Contributor III

Hey Lindsay,

I've seen issues with the keyring, where the Credentials Manager in Windows has corrupted the profiles.

Opening an GIS connection is successful, but you are not being authenticated as the user you think you are. It ends up making an anonymous connection instead. This would explain the failure to find the service resources.

You may need open the Credentials Manager from the Control Panel and drop the accounts, then use the Python API GIS connection object to recreate the Profile with the stored credentials.

LindsayRaabe_FPCWA
Occasional Contributor III

Hi. Interesting. I did use a code sample to use keyring to store the credentials in the first place - is this what you are referring to? I might try updating the details again anyway and see if I get a different outcome. 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

I tried removing the credentials and resaving them (using the below code) but it made no difference. 

### Set password and optionally verify storage
import keyring
from arcgis.gis import GIS
username = input("Enter username: ")
password = input("Enter password: ")
keyring.set_password("ArcGISOnline", username, password)
#Optionally verify password storage
print("Verifying credentials: attempting to login")
try:
    pw = keyring.get_password("ArcGISOnline", username)
    gis = GIS("https://org.maps.arcgis.com", username, pw)
    print("Connected to the GIS - credentials verified and stored.\n\n")
except:
    print("Login failed - invalid credentials stored. Re-run to update credentials.\n\n")
Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

Code sample below

 

from datetime import datetime, timedelta, date

# *****Update the below 7 lines*****

name = "ArcOnline Related Table" #Name of dataset for use in error email notification
url_fl = "https://services3.arcgis.com/1GMgEwg21aGmaBQf/arcgis/rest/services/ArcOnlineRelatedTable/FeatureServer/1" # Service URL for feature layer (TABLE/RELATED TABLE ONLY) to download as feature class
destGDB = r"\\a\folder\on\our\server\Data.gdb" #The GDB where the backup feature class will be created
destFC = "ArcOnlineRelatedTable" #The backup feature class name (no spaces - user _ or CamelCase)
monthlimit = datetime.today() - timedelta(days=30) # number of days to keep daily backups
yearlimit = datetime.today() - timedelta(days=365) # number of days to keep monthly backups (1st day of month only) - everything older will be deleted
timeout = 60 # number of seconds delay to allow the export to complete 

while True:  #If something fails in the main script under "try", the "except" section emails a notification to the GIS Inbox
   try:
      import arcpy
      from arcpy import env
      from arcgis import gis #Instead of signing in as follows (gis = GIS("https://org.maps.arcgis.com", "Username", "Password") this breaks it up so that you are able to call the sign in details independently
      from arcgis.gis import GIS
      from arcgis.features import FeatureLayer
      import getpass
      import requests
      from time import strftime
      import datetime
            
      ### Access the stored password with keyring and sign into the GIS      # https://community.esri.com/t5/arcgis-online-blog/connect-to-the-gis-in-python-scripts-without/ba-p/889867
      import keyring
      pw = keyring.get_password("ArcGISOnline", "my_username")
      gis = GIS("https://org.maps.arcgis.com", "my_username", pw)
      print("Connected to the GIS")

      # CREATING BACKUP OF FEATURE SERVICE TABLE / RELATED TABLE
      print ("Exporting backup of feature service")
      Outputfs = destFC + "_" + strftime("%Y%m%d_%H%M%S")
      arcpy.conversion.TableToTable(url_fl, destGDB, Outputfs)
      time.sleep(timeout) #add time delay to allow export to complete
      print (name + " feature service exported to backup GDB: " + destGDB + "\\" + Outputfs)
      
      print ("Filtering past backups")

      arcpy.env.workspace = destGDB
      FClist = arcpy.ListFeatureClasses(destFC + "*")

      for fc in FClist:
          datestamp = datetime.datetime.strptime(('{}'.format(fc))[-15:],  "%Y%m%d_%H%M%S")
          day = (datestamp.day)
          print (fc + "..........Backup date:" + str(datestamp))
          if datestamp < yearlimit: #data more than 365 days old
              print ("     Older than 12 months: delete backup")
              arcpy.management.Delete(fc)
              print ("Deleted")
          elif datestamp < monthlimit: #fc more than 90 days old
              if day == 1:
                  print ("     1st of month & 4-12 months old: retain as monthly backup") #fc from 1st of Month and more than 90 days old
              else:
                  print ("     Older than 3 months and not the 1st of the month: delete backup")
                  arcpy.management.Delete(fc) #fc NOT from 1st of Month and more than 90 days old
                  print ("Deleted")
          else:
              print ("     Less than 3 months old: retain backup")
      print ("Script finished")
      break # Stops script here
   except Exception as e:
      from email.mime.multipart import MIMEMultipart
      from email.mime.text import MIMEText
      import smtplib

      fromaddr = "me@org.wa.com.au"
      toaddr = "me@org.wa.com.au"
      msg = MIMEMultipart()
      msg['From'] = fromaddr
      msg['To'] = toaddr
      msg['Subject'] = name + " backup process failed"
      # Enter email body text below
      body = "There has been an error backing up the feature service. Please check the script to troubleshoot any problems. Exception: " + str(e)
      msg.attach(MIMEText(body, 'plain'))    
      server = smtplib.SMTP('smtp.org.com.au')
      #No login required so this section is commented out
      #server.login("youremailusername", "password")
      server.sendmail(fromaddr, toaddr, str(msg))
      print ("Script failed - email notification sent to me@org.com.au")
      print ("Exception: " + str(e))
      break # Stops script here

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos