0
I’m running on error here… I’m a beginner but I cant figure out what I am missing! The query runs fine on Data Explorer’s script editor.
influxdb 2.4
Python 3.10.6
Ubuntu 22.04.1
influxdb_client 1.32.0 (pip install ‘influxdb-client[extra]’)
I installed the package according to documentation
Any help is much appreciated, thank you.
import os
from influxdb_client import InfluxDBClient
token = os.getenv('INFLUXDB_V2_TOKEN')
org = os.getenv('INFLUXDB_V2_ORG')
bucket = os.getenv('INFLUXDB_V2_BUCKET')
url = os.getenv('URL')
client = InfluxDBClient(url=url, token=token,org=org)
query_api = client.query_api()
with client:
"""
Querying ingested data
"""
query = 'from(bucket:"{bucket}")' \
' |> range(start: 0, stop: now())' \
' |> filter(fn: (r) => r._measurement == "financial-analysis-df")' \
' |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' \
' |> limit(n:10, offset: 0)'
"""
Query: using Pandas DataFrame
"""
df = query_api.query_data_frame(query)
print(df.to_string())
"""
Close client
"""
client.close()
Hi @yannisant,
Can you try something like this instead:
import os
from influxdb_client import InfluxDBClient
token = os.getenv('INFLUXDB_V2_TOKEN')
org = os.getenv('INFLUXDB_V2_ORG')
bucket = os.getenv('INFLUXDB_V2_BUCKET')
url = os.getenv('URL')
client = InfluxDBClient(url=url, token=token,org=org)
query_api = client.query_api()
with client:
"""
Querying ingested data
"""
q = '''
from(bucket:"_bucket")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r._measurement == "financial-analysis-df")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> limit(n:10, offset: 0)
'''
p = {
"_bucket": bucket,
}
"""
Query: using Pandas DataFrame
"""
df = query_api.query_data_frame(query=q, params=p)
print(df.to_string())
"""
Close client
"""
client.close()
Hello @Jay_Clifford
,
thank you for your message. Unfortunately this doesn’t work.
Hi @yannisant,
Can you provide the error message you receive? Its hard for me to debug the issue without that 
Here’s the error message:
Traceback (most recent call last):
File "/home/yannis/webapp/influxdb/query_db.py", line 9, in <module>
client = InfluxDBClient(url=url, token=token,org=org)
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/client/influxdb_client.py", line 57, in __init__
super().__init__(url=url, token=token, debug=debug, timeout=timeout, enable_gzip=enable_gzip, org=org,
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/client/_base.py", line 56, in __init__
if self.url.endswith("/"):
AttributeError: 'NoneType' object has no attribute 'endswith'
Exception ignored in: <function InfluxDBClient.__del__ at 0x7fe7ae4681f0>
Traceback (most recent call last):
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/client/influxdb_client.py", line 283, in __del__
if self.api_client:
AttributeError: 'InfluxDBClient' object has no attribute 'api_client'
I’ve found the problem
the issue is with os. Once I declared the authentication variables in the same file everything worked… I will investigate other authorisation methods.
1 Like