DataStax Python driver

To use the DataStax Python driver, you need to install the driver and its dependencies, and then connect the driver to your Astra DB Classic database. Once connected, you can write scripts that use the driver to run commands against your database.

Prerequisites

  1. Create a database.

  2. Set the following environment variables:

    • ASTRA_DB_ID: The database ID

    • ASTRA_DB_REGION: A region where your database is deployed and where you want to connect to the database, such as us-east-2

    • ASTRA_DB_KEYSPACE: A keyspace in your database, such as default_keyspace

    • ASTRA_DB_APPLICATION_TOKEN: An application token with the Database Administrator role.

      The token.json has the following format:

      {
        "clientId": "CLIENT_ID",
        "secret": "CLIENT_SECRET",
        "token": "APPLICATION_TOKEN"
      }

      For driver authentication, you can use either clientId and secret or the literal string token and the AstraCS token value. If you are on an older driver version that doesn’t support the token option, then you might need to use clientId and secret. For more information, see Token details.

  3. Download your database’s Secure Connect Bundle (SCB).

  4. Download and install a supported Python version. Python 3.4, 3.5, 3.6, 3.7, and 3.8 are supported, and CPython (the standard Python implementation) and PyPy are supported and tested.

Install the Python driver

  1. Install the DataStax Python driver:

    pip install cassandra-driver

    Make sure you use a driver version that is compatible with Astra DB. For more information, see DataStax driver matrix.

  2. Verify the installation:

    pip show cassandra-driver

    Make sure the returned Version is the latest version or the specific version that you installed.

Connect the Python driver

  1. In the root of your Python project, create a connect_database.py file:

    cd python_project
    touch connect_database.py
  2. Copy the following connection code into connect_database.py, and then replace PATH_TO_SCB with the absolute path to your database’s Secure Connect Bundle (SCB) (secure-connect-DATABASE_NAME.zip):

    connect_database.py
    import os
    from cassandra.cluster import Cluster
    from cassandra.auth import PlainTextAuthProvider
    import json
    
    with open(ASTRA_TOKEN_PATH, "r") as f:
        creds = json.load(f)
        ASTRA_DB_APPLICATION_TOKEN = creds["token"]
    
    cluster = Cluster(
        cloud={
            "secure_connect_bundle": PATH_TO_SCB,
        },
        auth_provider=PlainTextAuthProvider(
            "token",
            ASTRA_DB_APPLICATION_TOKEN,
        ),
    )
    
    session = cluster.connect()

    This code creates a Cluster instance to connect to your Astra DB database. You typically have one instance of Cluster for each Astra DB database that you want to interact with.

  3. Add code that runs a CQL query and prints the output to the console:

    connect_database.py
    row = session.execute("select release_version from system.local").one()
    if row:
        print(row[0])
    else:
        print("An error occurred.")
  4. Save and run your Python script:

    python ./connect_database.py

    The output prints the release_version value from the system.local table in your Astra DB database.

  5. Extend or modify this script to run other commands against your database or connect to other databases. For more information, see Python driver documentation and Developing applications with DataStax drivers.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com