ArcGIS API for Python

2.4.2 · active · verified Fri Apr 17

The ArcGIS API for Python is a powerful, open-source library for working with GIS data and services. It provides a rich set of tools for spatial analysis, data management, and mapping, enabling automation and scripting workflows with ArcGIS Online and ArcGIS Enterprise portals. The current version is 2.4.2, and it follows a regular release cadence with several updates per year, often aligning with major ArcGIS platform releases.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to connect to ArcGIS Online or an ArcGIS Enterprise portal using the GIS object. It shows how to authenticate and perform a basic content search. It is highly recommended to use environment variables for sensitive credentials.

import os
from arcgis.gis import GIS

# Connect to ArcGIS Online or a specific portal
# It's recommended to store credentials securely, e.g., using environment variables or secret management.
# For ArcGIS Online, use 'https://www.arcgis.com'
# For ArcGIS Enterprise, use your portal URL, e.g., 'https://yourportal.com/portal'

# Option 1: Anonymous access (limited)
# gis = GIS()

# Option 2: Connect using username and password (for scripting)
# Replace with your actual credentials or retrieve from secure storage
portal_url = os.environ.get('ARCGIS_PORTAL_URL', 'https://www.arcgis.com')
username = os.environ.get('ARCGIS_USERNAME', 'YOUR_USERNAME')
password = os.environ.get('ARCGIS_PASSWORD', 'YOUR_PASSWORD')

if username == 'YOUR_USERNAME' or password == 'YOUR_PASSWORD':
    print("Warning: Please set ARCGIS_USERNAME and ARCGIS_PASSWORD environment variables or replace placeholders.")
    # Attempt anonymous access if credentials aren't set
    try:
        gis = GIS(portal_url)
        print("Connected anonymously to " + gis.url)
    except Exception as e:
        print(f"Failed to connect anonymously: {e}")
        gis = None
else:
    try:
        gis = GIS(portal_url, username, password)
        print("Logged in as " + gis.properties.user.username + " to " + gis.url)
    except Exception as e:
        print(f"Failed to connect with credentials: {e}")
        gis = None

if gis:
    # Example: Search for content
    items = gis.content.search(query="type:'Web Map'", num_items=5)
    for item in items:
        print(f"- {item.title} ({item.owner})")

view raw JSON →