Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

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

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):

#!/usr/bin/python
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 at 16:00

Your Answer

 
discard

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.