replace Username/Password with SDE connection

05-03-2023 11:35 AM
Occasional Contributor III


I am currently doing the below


from arcgis.gis import GIS
import arcgis
gis = GIS('', agol_username, agol_password)



But I do not want to hard code the Username and Password in the script

Can I do something like point to an SDE connection?

I want a way to HIDE the credentials


from arcgis.gis import GIS
import arcgis
gis = GIS('', '\\DatabaseConnections\conectionfile.sde)


Or add in a workspace for this?


arcpy.env.workspace = "C:\\Users\\GIS_projects\\Python\\Test@EDIT.sde"


0 Kudos
3 Replies
MVP Frequent Contributor

One way, If you are running on a machine with Pro installed, and have logged into Pro within the last two weeks with the user credential that you want to pass, you can use:


gis = GIS("home")


This will run using the credentials of the last user to log into Pro under that profile.

I actually have a scheduled task that opens Pro and closes it every 10 days just to keep the credentials "active".


0 Kudos
Occasional Contributor III

Its on a server and no real direct access to remote on... so not sure of who credentials will be sitting there when I looks

0 Kudos
Occasional Contributor

@kapalczynski  I piecemeal solutions from different sources; you might want to check them out. 



  • I also would like to add that to set a password using keyring I ran Python Interactive Terminal (C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.bat) and followed the instructions on this video

This is what I came up with: 


import keyring
from arcgis.gis import GIS
from arcgis.gis.server import Server

# Specify the name of the profile
profile_name = "GIS_Admin"  # Replace with your profile name

# Get username and password stored in keyring

credentials = keyring.get_credential("keyring_portaladmin", username=None)
vusername = credentials.username
vpassword = credentials.password

# Connect to the ArcGIS Portal using the specified profile
gis = GIS(url="", username=vusername, password=vpassword,profile=profile_name)

# Check if the connection is successful
if gis:
    print("Connected to ArcGIS Portal as '{}'.".format(
    print("Failed to connect to ArcGIS Portal.")


Jen Zumbado-Hannibal, GISP
GIS Coordinator
City of Forest Grove
Forest Grove, OR 97116

0 Kudos