Unable to Install a Database via Python Script

Hello everybody - I’m new here and rather a beginner in python and Debian.
I have influxdb v2.7.6 running on a raspberrypi. It works fine with the pv-software evcc.
Now I want to install another influxdb-database via a python script to analyze our pellets heating (oekofen). To achieve this I started a python script I found in the installation guide for oekofen-spy that seems to work for influxdb v1 (https://gitlab.com/p3605/oekofen-spy/-/blob/main/Python3/init_influx_oekofen_spy.py).
Unfortunately this failed at the very first command: from influxdb import influxdbclient with the message: error: externally-managed-environment. The following messages are too complicated for me to understand.
I hope there might be an easy fix for this task?
Emilio

Hello @Style177 Welcome!
Can you utilize a virtual env?
Install venv (if not already installed):

sudo apt install python3-venv

In the directory where you’re working with your Python script, create a virtual environment:

python3 -m venv myenv

Once the virtual environment is created, you need to activate it:

source myenv/bin/activate

With the virtual environment activated, install the correct InfluxDB client for InfluxDB v2, which is influxdb-client instead of influxdb (used for v1):

pip install influxdb-client

Since you’re using InfluxDB v2, the influxdb-client library is the one you need. Update the import statement in your script:

from influxdb_client import InfluxDBClient

I think you might be using the wrong one :slight_smile:

Hi Anaisdg,
I’m really glad to find someone helping me through the jungle. For me as a old-fashioned windows-user, working on that raspberrypi is utterly challenging. Most of the time I’m doing things I don’t understand and sometimes it works out, sometimes not.
So, back from holiday I now tried to apply your proposed solution, but unfortunately ran into an error message that leaves me without a cue what to do:


Any idea what to do?
Emilio

Maybe a step forward(?):
I was finally able to install a virtual environment and to activate it. Also the pip-command to install influxdb-client seemed to have worked fine:


(sorry for posting jpg; copy/paste scrambles the coloured text)
But then, running my python script to create an influxdb database, the statement
from influxdb import influxdbclient produdes an error:
I get the message ModuleNotFoundError: No module named 'influxdb'.
I think, I should look for that module somewhere else, but where?
Emilio

For users with similar problems:
Finally, after spending some hours in the python/influx-jungle, I managed to get a solution:

After having installed a virtual environment via python3 -m venv myenv, and having created a bucket in influxdb manually, the following python-script sends data points to influxdb.

import influxdb_client, os, time
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

token = "<my_token>"
org = "<my_org>"
url = "http://raspberrypi:8086"

write_client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)

bucket="<my_bucket>"

write_api = write_client.write_api(write_options=SYNCHRONOUS)
   
for value in range(5):
  point = (
    Point("measurement1")
    .tag("tagname1", "tagvalue1")
    .field("field1", value)
  )
  write_api.write(bucket=bucket, org=org, record=point)
  time.sleep(1) # separate points by 1 second