Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say we are on a command line and we declare an environment variable:

export x=100

When i go to python and type :

os.environ['x'] = '100000'

If I came back to command line x stays 100. Is there any way to change this variable in python ?

share|improve this question

There is no way to change the environment variables from a child process. And there is an exactly same question on SO.

share|improve this answer
This is the correct answer. If you really need it, you'll have to start with a shell script, call python, and have your python communicate back to the shell script (there's multiple ways to do IPC, do some research and figure out what works for you). – Avindra Goolcharan Jan 8 at 16:23

There is a workaround to what you want to achieve. Assuming you use bash as your shell, you can write the changes to bash initialization files(I chose ~/.bashrc):

import os
os.system('bash -c \'echo "export a=100000" >> ~/.bashrc\'')
os.system('bash -c \'source ~/.bashrc\'')

You need to prepend bash -c, since python uses sh as the default shell.

Idea derived from a similar question on SO(different from the one falconer posted).

share|improve this answer
This append "a=100000" to ~/.bashrc file, but the "source" doesn't work. If I exit python and do: "echo $a" the variable isn't there. – Tickon Nov 29 '14 at 16:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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