Python client library
Use the InfluxDB Python client library to integrate InfluxDB into Python scripts and applications.
This guide presumes some familiarity with Python and InfluxDB. If just getting started, see Get started with InfluxDB.
Before you begin
Install the InfluxDB Python library:
pip install influxdb-client
Ensure that InfluxDB is running. If running InfluxDB locally, visit http://localhost:8086. (If using InfluxDB Cloud, visit the URL of your InfluxDB Cloud UI. For example: https://us-west-2-1.aws.cloud2.influxdata.com.)
Write data to InfluxDB with Python
We are going to write some data in line protocol using the Python library.
In your Python program, import the InfluxDB client library and use it to write data to InfluxDB.
import influxdb_client from influxdb_client.client.write_api import SYNCHRONOUS
Define a few variables with the name of your database (bucket), organization (required, but ignored), and token.
bucket = "DATABASE_NAME" org = "ignored" token = "DATABASE_TOKEN" # Store the URL of your InfluxDB instance url="https://cluster-id.influxdb.io"
Instantiate the client. The
InfluxDBClient
object takes three named parameters:url
,org
, andtoken
. Pass in the named parameters.client = influxdb_client.InfluxDBClient( url=url, token=token, org=org )
The
InfluxDBClient
object has awrite_api
method used for configuration.Instantiate a write client using the
client
object and thewrite_api
method. Use thewrite_api
method to configure the writer object.write_api = client.write_api(write_options=SYNCHRONOUS)
Create a point object and write it to InfluxDB using the
write
method of the API writer object. The write method requires three parameters:bucket
,org
, andrecord
.p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3) write_api.write(bucket=bucket, org=org, record=p)
Complete example write script
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
bucket = "DATABASE_NAME"
org = "ignored"
token = "DATABASE_TOKEN"
# Store the URL of your InfluxDB instance
url="https://cluster-id.influxdb.io"
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)
# Write script
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api.write(bucket=bucket, org=org, record=p)
Query data from InfluxDB with Python
The InfluxDB v2 Python client cannot query InfluxDB Cloud Dedicated. To query your dedicated instance, use a Flight SQL client with gRPC.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
InfluxDB Cloud and InfluxDB Enterprise customers can contact InfluxData Support.