3

I am having a problem with the environment variables in python. How do I get python to export variables to the parent shell?

I am using ubuntu, python 2.7.4

I get this:

$ python
>>> import os
>>> os.environ
{'HOME':'~'}
>>> os.environ['foo']='bar'
>>> os.environ
{'HOME':'~','foo':'bar'}
>>> quit()
$ echo $foo
 # Place #1
$ python
>>> import os
>>> os.environ
{'HOME':'~'} # Place #2
>>> 

My expected output is:

  • At Place #1: bar
  • At Place #2: {'HOME':'~','foo':'bar'}

Thanks

asked Jun 29, 2013 at 0:18
0

1 Answer 1

4

Environment variables set in a child process (e.g. python) do not affect the parent process.

It's a one-way street; if this could be done it would be very easy to exploit shells! The environment variables must be set in the parent process itself. This restriction is enforced by the operating system and is not specific to Python.

Note that sourcing a file in a shell (e.g. . script.sh) doesn't create a new process; but there is no way to "source" Python files.

answered Jun 29, 2013 at 0:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.