184

My python script which calls many python functions and shell scripts. I want to set a environment variable in Python (main calling function) and all the daughter processes including the shell scripts to see the environmental variable set.

I need to set some environmental variables like this:

DEBUSSY 1
FSDB 1

1 is a number, not a string. Additionally, how can I read the value stored in an environment variable? (Like DEBUSSY/FSDB in another python child script.)

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user749632
  • 1,989
  • 2
  • 12
  • 5
  • 16
    What's with the "DEBUSSY" thing? It was in the duplicate thread too. Google just brings up info on some French guy. – voithos May 11 '11 at 23:41
  • 19
    @voithos shame on you! – David Heffernan May 11 '11 at 23:50
  • 3
    @David What...? Okay, so my "French" comment was pointless, yes, but I'm still wondering what this DEBUSSY thing is. – voithos May 11 '11 at 23:53
  • Sorry guys, if i sounded lazy. The reason i got very less time to modify the scripts and so i am resorting to the forums. I like reading books. Just that lack of time is prompting me to find quick solutions on forums – user749632 May 12 '11 at 01:33
  • 14
    DEBUSSY is a tool which helps us to view waveforms while designing hardware systems. I used the real world example. I design Hardware systems but we use lot of scripting languages to automate the tool flow – user749632 May 12 '11 at 01:37

4 Answers4

341

Try using the os module.

import os

os.environ['DEBUSSY'] = '1'
os.environ['FSDB'] = '1'

# Open child processes via os.system(), popen() or fork() and execv()

someVariable = int(os.environ['DEBUSSY'])

See the Python docs on os.environ. Also, for spawning child processes, see Python's subprocess docs.

voithos
  • 68,482
  • 12
  • 101
  • 116
  • When I try this approach in mac os, the environment variables aren't available from the shell. only python can read the environment variables set this way. – openCivilisation Mar 16 '19 at 09:47
  • 1
    @openCivilisation That is correct, it should be impossible for a script to modify the calling shell's environment for security reasons (imposed by the shell, not python) – DBX12 Mar 10 '21 at 11:11
  • @DBX12 do you have a reference to this? – BND Feb 20 '23 at 11:55
  • @BND I guess it is written somewhere in the docs of the shell (e.g. Bash). I only know that limitation as a "core principle" like "you cannot edit file modes on files you don't own" – DBX12 Feb 20 '23 at 12:05
39

First things first :) reading books is an excellent approach to problem solving; it's the difference between band-aid fixes and long-term investments in solving problems. Never miss an opportunity to learn. :D

You might choose to interpret the 1 as a number, but environment variables don't care. They just pass around strings:

   The argument envp is an array of character pointers to null-
   terminated strings. These strings shall constitute the
   environment for the new process image. The envp array is
   terminated by a null pointer.

(From environ(3posix).)

You access environment variables in python using the os.environ dictionary-like object:

>>> import os
>>> os.environ["HOME"]
'/home/sarnold'
>>> os.environ["PATH"]
'/home/sarnold/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'
>>> os.environ["PATH"] = os.environ["PATH"] + ":/silly/"
>>> os.environ["PATH"]
'/home/sarnold/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/silly/'
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • I agree with you. Just that i have less time to fix this scripts and reading the book takes a week. I am going to read Python book to get good knowledge on programming in Python – user749632 May 12 '11 at 01:35
  • 8
    One learns python by doing, not reading – CpILL Apr 08 '19 at 06:27
3

If you want to pass global variables into new scripts, you can create a python file that is only meant for holding global variables (e.g. globals.py). When you import this file at the top of the child script, it should have access to all of those variables.

If you are writing to these variables, then that is a different story. That involves concurrency and locking the variables, which I'm not going to get into unless you want.

systemizer
  • 355
  • 2
  • 6
1

Use os.environ[str(DEBUSSY)] for both reading and writing (http://docs.python.org/library/os.html#os.environ).

As for reading, you have to parse the number from the string yourself of course.

piro
  • 13,378
  • 5
  • 34
  • 38