In my program, I retrieve a value from a global variable if I run a program in the python console everything works, I defined the global variable as follows: export GOOGLE_APPLICATION_CREDENTIALS = /home/pi/shared/service-account-file.json

The value is written in:

  • /etc/profile
  • /home/pi/.profile

However, I want the program to run periodically, using webmin I have added the program to run, but it always ends up loading errors in the global variable:

Traceback (most recent call last):
  File "/home/pi/shared/CloudRaspberry.py", line 20, in <module>
    db = firestore.Client()
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/firestore_v1/client.py", line 110, in __init__
    project=project, credentials=credentials, _http=None
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/client.py", line 226, in __init__
    _ClientProjectMixin.__init__(self, project=project)
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/client.py", line 178, in __init__
    project = self._determine_default(project)
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/client.py", line 193, in _determine_default
    return _determine_default_project(project)
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/_helpers.py", line 186, in _determine_default_project
    _, project = google.auth.default()
  File "/usr/local/lib/python3.7/dist-packages/google/auth/_default.py", line 321, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

/etc/profile and /home/pi/.profile are only taken into account by interactive shells. I'm pretty sure webmin isn't one, so it will not look there.

The easiest solution would be to specify the variable as a prefix for the command you're running. So, instead of telling webmin to run (for instance)

python /home/pi/shared/CloudRaspberry.py

you tell it to run

GOOGLE_APPLICATION_CREDENTIALS=/home/pi/shared/service-account-file.json python /home/pi/shared/CloudRaspberry.py

You may need to run bash or sh before python in your crontab to read in the environment variables you want.

Presumably at the moment you have something like

* * * * * /path/to/script.py > /path/to/script.log 2>&1

Whereas something like this may help read in the .profile

* * * * * /bin/bash -c /path/to/script.py > /path/to/script.log 2>&1

  • It's also worth pointing out that, for the sake of defining a couple of variables, there's also the option of adding them to the crontab preamble too: see linux.die.net/man/5/crontab. – Roger Jones Feb 23 '20 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.