Python

This section is intended to help Python developers get started using the Fauna Python driver for application development.

Current version and repository location

Current stable version

1.1.0

Repository

Installation

pip install faunadb

Compatibility

The following versions of Python are supported:

  • Python 3.5

  • Python 3.6

  • Python 3.7

  • Python 3.8

  • Python 3.9

Example application

The following example code runs a bare-bones Python application which creates a new document in a collection called scores.

Prerequisites

  • A Fauna account. If you don’t have one, see the dashboard quick start for help getting set up.

  • Python 3.5 or higher and the Fauna Python driver. See Installation for driver installation help.

Procedure

  1. Navigate to the Fauna Dashboard

    Log in to your Fauna account at Fauna Dashboard if you’re not already logged in.

  2. Create a new database

    Click NEW DATABASE and select your Region Group.

  3. Create a new collection

    Click NEW COLLECTION. Name your new collection People and save it.

  4. Create an access key

    Click SECURITY in the left-side navigation menu. Create a new key for your database. Be sure to save the key’s secret in a safe place, as it is only displayed once.

  5. Create a local environment variable with your access key’s secret

    On MacOS and Linux systems, enter the following in a terminal window:

    export FAUNADB_SECRET=<your-secret>

    For Windows systems, enter the following in a terminal window:

    set FAUNADB_SECRET=<your secret>

    For either example, replace <your secret> with the secret for the access key that you created.

  6. Create a local application file

    With your preferred editor, create a file called test.py with the following contents:

    #!/usr/bin/env python3
    
    import os
    import sys
    from urllib.parse import urlparse
    from faunadb import query as q
    from faunadb.client import FaunaClient
    
    secret = os.getenv("FAUNADB_SECRET")
    endpoint = os.getenv("FAUNADB_ENDPOINT")
    
    if not secret:
      print("The FAUNADB_SECRET environment variable is not set, exiting.")
      sys.exit(1)
    
    endpoint = endpoint or "https://db.fauna.com/"
    
    o = urlparse(endpoint)
    
    client = FaunaClient(
      secret=secret,
      endpoint=endpoint
    )
    
    result = client.query(
      q.create(
        q.collection("People"),
        {
          "data": {
            "first": "Linus",
            "last": "Torvalds",
            "age": 52,
          }
        }
      )
    )
    print(result)

    The above example app uses the Create function to create a new document in the scores collection.

  7. Run your application

    In a terminal window, run the application:

    python test.py

    You should see results similar to this:

    {'ref': Ref(id=324881028575396352, collection=Ref(id=People, collection=Ref(id=collections))), 'ts': 1646089542840000, 'data': {'first': 'Linus', 'last': 'Torvalds', 'age': 52}}

    If you get an error, check to make sure your access key is correct.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!