# Unable to Install a Database via Python Script

**URL:** <https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520>\
**Category:** InfluxDB 2\
**Created:** [September 20, 2024, 9:30pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520 "2024-09-20T21:30:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Style177](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/style177/32/16534_2.png) [@Style177](https://community.influxdata.com/u/Style177)\
**Post date:** [September 20, 2024, 9:30pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520/1 "2024-09-20T21:30:43Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [September 24, 2024, 11:51pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520/2 "2024-09-24T23:51:05Z")

</div>

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

```auto
sudo apt install python3-venv

```

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

```auto
python3 -m venv myenv

```

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

```auto
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):

```auto
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:

```auto
from influxdb_client import InfluxDBClient

```

> **[GitHub - influxdata/influxdb-client-python: InfluxDB 2.0 python client](https://github.com/influxdata/influxdb-client-python)**
>
> InfluxDB 2.0 python client

I think you might be using the wrong one 🙂

---

<div class="post-metadata">

**Author:** ![Style177](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/style177/32/16534_2.png) [@Style177](https://community.influxdata.com/u/Style177)\
**Post date:** [October 1, 2024, 10:11pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520/3 "2024-10-01T22:11:53Z")

</div>

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:

 ![venv-try-install](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/3X/3/5/351021e7106a93366ef3421ec1f348f241a353f5.jpeg)  
Any idea what to do?  
Emilio

---

<div class="post-metadata">

**Author:** ![Style177](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/style177/32/16534_2.png) [@Style177](https://community.influxdata.com/u/Style177)\
**Post date:** [October 2, 2024, 4:56pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520/4 "2024-10-02T16:56:42Z")

</div>

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:

 ![install-influxdbclient](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/3X/a/b/ab35822b4510a1e18e5a8e63d77fc51eb54c5e92.jpeg)  
(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

---

<div class="post-metadata">

**Author:** ![Style177](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/style177/32/16534_2.png) [@Style177](https://community.influxdata.com/u/Style177)\
**Post date:** [October 3, 2024, 7:22pm UTC](https://community.influxdata.com/t/unable-to-install-a-database-via-python-script/37520/5 "2024-10-03T19:22:09Z")

</div>

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.

```auto
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

```
