0

I am sorry if my question not very clear.

I am trying to create a variable pass it to the environment in linux. Then, I want to be able to get this variable some where else. What i have tried so far on the linux command line:

local_pc:~/home$ export variable=10
local_pc:~/home$ python -c 'import os; print os.getenv("variable")'
10

which all sound fine. But when I set export in python I will not be able to get it

subprocess.call(["export","variable=20"],shell = True)
print(os.getenv("variable"))
None

So my question here is how to do xport variable=10 in python

Joe
  • 46,419
  • 33
  • 155
  • 245
Mero
  • 1,280
  • 4
  • 15
  • 25
  • You could use `subprocess.Popen` to pipe output from `send.py` to `receive.py`, and write the environment variable and its value onto the pipe. `receive.py` would then need to read this data from the pipe and change its environment appropriately. Not sure what else you need here. – Tim Pierce Dec 11 '13 at 17:08
  • 2
    Could you provide more details? Don't concentrate on how to do it, just describe what you want to do (in plain English), then show what have you tried (minimal but complete code example) and in what way it failed (input/output, tracebacks if any). – jfs Dec 11 '13 at 17:17
  • Hi, I am sorry if my question not very clear. – Mero Dec 11 '13 at 17:25
  • why do you need to change an environment variable instead of, for example, passing a value as a command line argument or writing it via subprocess' standard input or using pipes, sockets explicitly or any other interprocess communication method? – jfs Dec 11 '13 at 18:58
  • Environment variables are not a feasible way to do interprocess communications. Here's a nice overview of basic IPC; perhaps one of these will be useful to you. http://www.tldp.org/LDP/lpg/node7.html – steveha Dec 11 '13 at 22:18

2 Answers2

2

You can change environment variable only for current process or its children. To change environment in its parent process would require hacks e.g., using gdb.

In your example export variable=10 is run in the same process and python -c .. command is a child process (of the shell). Therefore it works.

In your Python example, you are trying (incorrectly) to export variable in a child process and get it in a parent process.

To summarize:

  • working example: parent sets environment variable for a child
  • non-working example: child tries to set environment variable for a parent

To reproduce your bash example:

import os
import sys
from subprocess import check_call

#NOTE: it works but you shouldn't do it, there are most probably better ways    
os.environ['variable'] = '10' # set it for current processes and its children
check_call([sys.executable or 'python', '-c', 
            'import os; print(os.getenv("variable"))'])

Subprocess either inherits parent's environment or you could set it explicitly using env argument.

For example, to change a local timezone that is used by time module, you could change TZ environment variable for the current python process on posix systems:

import os
import time

os.environ['TZ'] = ':America/New_York'
time.tzset()

is_dst =  time.daylight and time.localtime().tm_isdst > 0 
# local time = utc time + utc offset
utc_offset = -time.timezone if not is_dst else -time.altzone
print("%s has utc offset: %.1f hours" % (
    os.environ.get('TZ').lstrip(':'), utc_offset/3600.))
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • note: `time.daylight` might be wrong in [some edge cases](http://www.selenic.com/pipermail/mercurial-devel/2011-November/035657.html). – jfs Apr 16 '14 at 23:48
1

update:

There's no feasible way to establish commuication over processes through environment variables.

The child may inherit the environment variables from the parent, but a change to the parent environment variables after the childs invocation won't be passed through to the child and the child's environment changes are completely intransparent to the parent. So no way!

I tested it by trying to establish a round-robin, token-based message passing approach, but i don't see a way to get the changes passed between the process environment variables.

Don Question
  • 11,227
  • 5
  • 36
  • 54