Import InfluxDBClient fails in python code

New to all this so please consider my ignorance.
Background: I have PI Zero W working with several weather sensors and wanted to visualise the data with Grafana. I thought the way to go was to use InfluxDB to create the database for it and have installed both Grafana and InfluxDB (1.8.0), linked them and started to mod my python code to start collecting. However the code errors immediately with:

from influxdb import InfluxDBClient

ModuleNotFoundError: No module named ‘influxdb’

However if I run the ‘from influxdb import InfluxDBClient’ command from the terminal there appears to be no problem?
What could be wrong?
Thanks

Hello @peterb,
Welcome! Please excuse me if this is a silly question, but have you installed with pip install influxdb-client ?

Thanks!
Yes I had but it made me think should it have been pip3?
So I have just tried that and that seems to made the difference, thanks for your suggestion!

Spoke too soon, it is now failing again with the same error!

How can that happen? Still struggling!

Sounds like a Python environment/dependency management issue rather than anything to do with Influx itself.

Is it possible that you’re running under different users, and influxdb-client is installed for some but not for others? Either way, it sounds like you’re switching Python environments. Best to create a single virtual environment where you install your package dependencies, and then always run in that environment. See e.g. https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Thanks for your input.
There is only one user and as far as I know, there is just one environment. This Pi is dedicated to this function so unless I have inadvertently created those things…? How can I check?

Ok, I think I see the problem - hinted at by @Anaisdg’s post in the discussion that somehow got “forked” into the other thread.

Option 1: Legacy client
To use the legacy (Influx 1.x focused) client, you need to install with

pip3 install influxdb

to get this package: https://pypi.org/project/influxdb/, and then when you run Python you should be able to import it with

from influxdb import InfluxDBClient

as you’ve been attempting so far.

Option 2: New client
If you’d like to use the new (Influx 2.0 focused) client, you need to install with

pip3 install influxdb-client

to get this package: https://pypi.org/project/influxdb-client/, and then when you run Python you should be able to import it with

import influxdb_client

In short, I think what happened is that you’ve installed the new Python package, but are trying to import the legacy one (which isn’t installed). If you’re running an Influx 1.8 server (as I gather from your other thread), then you should be able to use either package. It’s mostly down to whether you’d like to write your queries using Flux - in which case use the new client - or InfluxQL - in which case you should use the legacy client. Flux is very powerful, but if you’re looking to do something reasonably simple then InfluxQL will probably be fine.

Thanks again for your input. Apologise for confusing by using more than one thread, perhaps a Moderator could sort that for me. I will post a ‘moved to here’ comment to the other one.

I am using influxdb 1.8 and it is active as confirmed by my command line entries, influx

Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0

and sudo systemctl status influxdb

● influxdb.service - InfluxDB is an open-source, distributed, time series databa
Loaded: loaded (/lib/systemd/system/influxdb.service; enabled; vendor preset:
Active: active (running) since Sat 2020-05-09 19:38:03 BST; 2 days ago
Docs: https://docs.influxdata.com/influxdb/
Main PID: 23411 (influxd)
Memory: 76.4M
CGroup: /system.slice/influxdb.service
└─23411 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

All this was fairly straightforward and trouble free. It is when I add the:

import influxdb_client

line into my code I get an error.

sudo python3 app_280.py
Traceback (most recent call last):
File “app_280.py”, line 27, in
from influxdb import InfluxDBClient #unremark to test
ModuleNotFoundError: No module named ‘influxdb’

Hope this helps clarify.

Entry made in error deleted

Two points:

  1. Can you clarify whether you’re trying to use the legacy or the new client? In your last post you say that you get an error when you add import influxdb_client, but the actual error you quote is with respect to the influxdb module not being found (not the influxdb_client).
  2. You’re running your Python code with sudo, i.e. as the root user. Is there a reason for doing that? It’s likely to complicate your package installation situation, and could be the reason for packages not being found - e.g. if you ran pip3 install ... to install packages as your regular user, but then run sudo python ..., the installed packages won’t be found.

Thanks for continuing with this for me. Re-reading my last post I did not make it clear, I blame the Grandchildren, continuously interrupting me, the joys of lockdown in a family home!
Taking point 2. first. My app_280.py is a modified version of one written by someone else which acts a web server to display data collected to a webpage with PIs IP address, he told me it needed sudo because of this, I just followed his advice. Being a novice I might have used it when I shouldn’t have in some subsequent steps with INfluxdb. Presumably I can check file ownerships and alter them, or would it be better just to do it again?

Point 1. It was the legacy version of influxdb_client I downloaded, running pip3 install… again I get:

pip3 install influx-client
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: influx-client in /usr/local/lib/python3.7/dist-packages (1.9.0)
Requirement already satisfied: simplejson in /usr/lib/python3/dist-packages (from influx-client) (3.16.0)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from influx-client) (2.21.0)
Requirement already satisfied: pytool in /usr/local/lib/python3.7/dist-packages (from influx-client) (3.15.0)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from pytool->influx-client) (1.12.0)

which I have seen several times now.
The influxdb-client-python github source says to follow the pip3 install… command with

import influxdb_client

This errors with bash:

import command not found

Ok, thanks for the explanation.

Since you’re running python3 with sudo, you also need to run pip3 with sudo - in order to install the package for the root user. Try

sudo pip3 install influx-client

Finally, import influxdb_client is the command that’s run in your Python script (as I think you realize) rather than from your bash prompt.

Thanks.
I ran the sudo pip3 line and it came back with already satisfied lines, as before
Unremarked my import line in my code and it came back with this error.

sudo python3 app_280.py
Traceback (most recent call last):
File “app_280.py”, line 27, in
from influxdb import InfluxDBClient #unremark to test
ModuleNotFoundError: No module named ‘influxdb’

Could this be something to do with my apps scope? Not sure if this is the correct terminology! My app is not aware of all the recent installs.

It looks like there is still some confusion regarding the legacy vs new version of the package you’re installing and respectively running. I suspect what you’ve done is install the new version (which is what you were importing in your code above), but it now looks like you’re trying to import the legacy version (influxdb), which is causing an error.

Have you tried running the following?

sudo pip3 install influxdb

Thanks again, must be hard trying sort this at a distance.
I don’t think I had run that so I did and got a list of already satisfied lines, a couple of download lines with Successfully installed influxdb-5.3.0 msgpack-0.6.1 as the final line.
I then ran my code from the command line and no error, brilliant !

So which element was legacy and how did I do it, was it using pip instead of pip3?

Now I will have to learn how to get my code to populate the database, then onto Grafana!!
Good job I have some time. Thanks so much.

Hi @peterb glad you got it working!

From what I saw, there were three issues:

  • pip3 needed to be used instead of pip
  • pip3 needed to be run with sudo, since python3 was being run with sudo
  • The legacy package influxdb needed to be installed, since that’s what the code uses (instead of the newer influx-client package)