> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Query e6data from Python

> Install the Python connector, connect to a cluster, and run SQL from your own code.

export const Outcome = ({children}) => <div className="e6-outcome">
    <span className="e6-outcome-label">When you finish</span>
    <div className="e6-outcome-body">{children}</div>
  </div>;

export const HowtoMeta = ({applies, owner}) => <div className="e6-meta">
    <span className="e6-eyebrow">How-to guide</span>
    {applies ? <span>{applies}</span> : null}
    {owner ? <span>Owner: {owner}</span> : null}
  </div>;

<HowtoMeta applies="All deployments" owner="Query team" />

<Outcome>Your script connects to a cluster, runs a query, and prints the rows.</Outcome>

## Before you start

You need:

* A running Query Engine cluster. On CloudPrem, the cluster also needs ingress. See [Configure cluster ingress](/query/clusters/ingress).
* A personal access token. See [Create, rotate and revoke tokens](/platform/tokens/create-rotate-revoke).
* Python 3, with a C++ compiler. On Windows, install Visual C++ Build Tools first (see [Windows setup](#windows-setup) below).

Find the host, port and cluster name in the console under **Cluster → Connectivity**.

<Steps>
  <Step title="Install the connector">
    Install the package from PyPI.

    ```bash terminal theme={"theme":{"light":"github-light","dark":"github-dark"}}
    pip install --no-cache-dir e6data-python-connector
    ```

    On Amazon Linux, CentOS, Ubuntu or Debian, install the build dependencies first if the install fails:

    <CodeGroup>
      ```bash Amazon Linux / CentOS theme={"theme":{"light":"github-light","dark":"github-dark"}}
      yum install python3-devel gcc-c++ -y
      pip install wheel
      ```

      ```bash Ubuntu / Debian theme={"theme":{"light":"github-light","dark":"github-dark"}}
      apt install python3-dev g++ -y
      pip install wheel
      ```
    </CodeGroup>
  </Step>

  <Step title="Set your credentials">
    Set the cluster host, your e6data email and your access token as environment variables. Do not hardcode them in your script.

    ```bash terminal theme={"theme":{"light":"github-light","dark":"github-dark"}}
    export E6_HOST="<cluster host>"
    export E6_USER="you@company.com"
    export E6_TOKEN="<access token>"
    ```
  </Step>

  <Step title="Connect and run a query">
    Open a connection to the cluster, create a cursor on your catalog, and run SQL.

    ```python query.py theme={"theme":{"light":"github-light","dark":"github-dark"}}
    import os
    from e6data_python_connector import Connection

    conn = Connection(
        host=os.getenv("E6_HOST"),
        port=443,                         # use the port shown under Cluster → Connectivity
        username=os.getenv("E6_USER"),    # your e6data email
        password=os.getenv("E6_TOKEN"),   # a personal access token
        database="<schema>",
        cluster_name="<cluster_name>",
        secure=True,                      # use TLS
    )

    cursor = conn.cursor(catalog_name="<catalog>")
    query_id = cursor.execute("SELECT * FROM <table> LIMIT 10")
    for row in cursor.fetchall():
        print(row)
    ```

    <Tip>
      **Large results.** Use `cursor.fetchmany(500)` to limit rows, or loop over `cursor.fetchall_buffer()` to stream rows without loading them all into memory.
    </Tip>
  </Step>

  <Step title="Close the connection">
    Clear and close the cursor, then close the connection. This frees old results from memory.

    ```python query.py theme={"theme":{"light":"github-light","dark":"github-dark"}}
    cursor.clear()   # not needed if you aborted the query
    cursor.close()
    conn.close()
    ```
  </Step>
</Steps>

## Windows setup

The connector uses a C-based deserialization library (`fastbinary`). On Windows it works only when Visual C++ Build Tools are installed. Without them you see this error:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
ImportError: cannot import name 'fastbinary' from 'thrift.protocol'
```

To fix it:

1. Install [Visual C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) and select the **Desktop Development with C++** workload. Launch the Build Tools once after installing.
2. Create and activate a virtual environment:
   ```bat theme={"theme":{"light":"github-light","dark":"github-dark"}}
   python -m venv <env_name>
   <env_name>\Scripts\activate.bat
   ```
3. Install the connector in that environment:
   ```bat theme={"theme":{"light":"github-light","dark":"github-dark"}}
   pip install --no-cache-dir e6data-python-connector
   ```

If you can't install the build tools, pass `require_fastbinary=False` to `Connection`. The connector then uses a pure-Python deserializer, which is about 2 to 3 times slower but otherwise works the same.

## Related

<Columns cols={3}>
  <Card title="Python connector reference" href="/query/connectivity-reference/python-connector">
    Reference · Connection parameters and the cursor API.
  </Card>

  <Card title="REST API for SQL queries" href="/query/connect/rest-api-for-sql">
    How-to · A driverless alternative over HTTPS.
  </Card>

  <Card title="Personal access tokens" href="/platform/tokens/pat-and-service-account-keys">
    Platform · Tokens the connector signs in with.
  </Card>
</Columns>

Source code: [e6data-python-connector on GitHub](https://github.com/e6x-labs/e6data-python-connector) · Package: [PyPI](https://pypi.org/project/e6data-python-connector/)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.