3

I am adding an environmental variable in bashrc, but am unable to see the variables using os.environ.get in a Python file.

I am using Raspbian on a Raspberry Pi 4.

I am setting an environmental variable in “bashrc” as follows:

export DB_USER='[email protected]'

When calling the following on Terminal:

$ env

…I find DB_USER in a the list of 24 items.

However, when I use the following in a Python file (this file is called by a bash script):

import os
...
try:
    with open("tempFile.txt", "a") as f:
        f.write(str(os.environ))
        f.close()
except FileNotFoundError:
    print("FileNotFoundError")
except IOError:
    print("IOError")

then ‘DB_USER’ is not in the list of 11 entries in "tempFile.txt".

How can I access the list of 24 items so that I can use ‘DB_USER’ entry?

Thanks

7
  • 3
    How are you running the Bash script that calls the Python script? From the command line? Or some other method? Commented Nov 6, 2022 at 16:26
  • 1
    bashrc is available only in a bash environment. See here. Therefore the question of @MattDMo. Also, check whether you start the script as normal user or via sudo. Commented Nov 6, 2022 at 18:19
  • @MattDMo - The Python script it being called by a Bash script. In turn, the Bash script is being called by a service. Commented Nov 6, 2022 at 18:22
  • 1
    Check out serverfault.com/questions/413397/… for some possible solutions. My Pi is currently AWOL, so I can't verify if Raspbian uses systemd, but the google machine says it does, so the first solution in the first answer should work. Commented Nov 6, 2022 at 18:29
  • 2
    The service isn't inheriting its environment from your shell, which means Python isn't either. Commented Nov 6, 2022 at 18:35

3 Answers 3

1

Since this is a service (so, not your user, as per Chepner's comment) calling something this looks like you want to make the environment variable available system-wide.

/etc/environment may fit your needs. You would just add

[email protected]

to it. (no, don't use export)

See also https://superuser.com/questions/664169/what-is-the-difference-between-etc-environment-and-etc-profile and https://raspberrypi.stackexchange.com/questions/37771/setting-system-wide-path-not-working-in-etc-environment (which talks about using /etc/profile.d instead, but similar concept - that's probably the approach I'd take, after testing a basic /etc/environment based fix)

Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this:

import os, subprocess

# try to simulate being the user so you can import/capture the env as that user
cmd = 'env -i sh -c ". /home/user/.bashrc && env"'

try:
    with open("tempFile.txt", "a") as f:
        for line in subprocess.getoutput(cmd).split("\n"):
        f.write(str(line))
        f.close()
except FileNotFoundError:
    print("FileNotFoundError")
except IOError:
    print("IOError")

Comments

0

I’ve managed to find a solution thanks to the suggestions given in this post.

I’m currently using /etc/environment in a script as follows :

  # get environmental variables
  source /etc/environment
  
  afile='<file path and name>‘

  date >> $afile
  
  echo >> $afile
  echo Straight from set >> $afile
  echo $DB_USER_environment >> $afile
  echo >> $afile
  
  echo Via a variable in the script >> $afile
  db_user02=$DB_USER_environment
  echo $db_user02 >> $afile
  
  echo >> $afile

This adds the following to afile

Sat 12 Nov 11:56:11 GMT 2022

Straight from set
[email protected]

Via a variable in the script
[email protected]

I intend to look at the etc/profile.d (@JL Peyret), Python (@TheAnalogyGuy) and systemd (@MattDMo) suggestions.

Thanks for all the replies

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.