> For the complete documentation index, see [llms.txt](https://docs.espresso.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.espresso.ai/databricks-optimizer/proxy-onboarding/proxy-onboarding-python.md).

# Python

If you have custom Python code using the [Databricks SQL Connector for Python](https://docs.databricks.com/aws/en/dev-tools/python-sql-connector), change `server_hostname` to the Espresso AI hostname and leave `http_path` unchanged, e.g.

```python
from databricks import sql

with sql.connect(
    server_hostname="<workspace>.cloud.databricks.espressocomputing.com",
    http_path="/sql/1.0/warehouses/<warehouse_id>",
    access_token="<personal_access_token>",
) as connection:
    with connection.cursor() as cursor:
        cursor.execute("SELECT 1")
        print(cursor.fetchall())
```

## OAuth for service principals (M2M)

With OAuth M2M, only the SQL connection needs the Espresso AI hostname. The credential provider can keep pointing at your Databricks workspace URL:

```python
from databricks.sdk.core import Config, oauth_service_principal
from databricks import sql

def credential_provider():
    config = Config(
        host="https://<workspace>.cloud.databricks.com",
        client_id="<client_id>",
        client_secret="<client_secret>",
    )
    return oauth_service_principal(config)

with sql.connect(
    server_hostname="<workspace>.cloud.databricks.espressocomputing.com",
    http_path="/sql/1.0/warehouses/<warehouse_id>",
    credentials_provider=credential_provider,
) as connection:
    # ...
```

## OAuth for users (U2M)

```python
from databricks import sql

with sql.connect(
    server_hostname="<workspace>.cloud.databricks.espressocomputing.com",
    http_path="/sql/1.0/warehouses/<warehouse_id>",
    auth_type="databricks-oauth",
) as connection:
    # ...
```
