I have a bunch of python scripts. One of them installs software (subversion) that requires it's path to be added to $PATH. After it is installed, I want the next script to use the software. If I run export PATH=/opt/subversion/bin:$PATH in bash between the first and second script, all is ok. But if I add os.system( 'export PATH=/opt/subversion/bin:$PATH' ) as the last command of the first script (that installs subversion), $PATH remains unaltered after it exits.

Is it any way to change $PATH from within python script so it will remain changed after the script finishes (inside single bash session, of course, I know about /etc/profile).


os.system() spawns a sub-shell. So it has no knowledge of your existing shell.

You can of course set the path (without exporting) when running the command. Ie:

PATH="$PATH:/opt/subversion/bin" /command/to/run

Oh, and os.system() is being depreciated. You should switch to subprocess.Popen().

  • Any way to emulate shell script "export" behavior for python script? – grigoryvp Jul 8 '09 at 21:25
  • Or any way to modify OS so new processes (os.system()) will use ew path? Just updating /etc/profile won't work: until console is restarted, all scripts and os.system() calls from that console will use old $PATH value :( – grigoryvp Jul 8 '09 at 21:41
  • Not that I'm aware of. Nor do I imagine it'd be adviseable. – Dan Carley Jul 8 '09 at 22:01

Generally, that is not possible. One process cannot change the environment of another process.

A parent can preset the environment of a child process it spawns, but there really isn't any way for one process to influence another's environment unless those applications are specifically designed to allow this.


As others have pointed out, no there's not really any way of modifying the environment of a parent process. It'd requrie fiddling around inside parent process' memory, and as far as I know there's no well defined way of doing this (I.e. you'd likely crash your shell).

If the scripts/programs you run afterwards is python, you could as the first thing you do in those programs, prepend the path to subversion into their environment.

import os
os.environ['PATH'] = "/opt/subversion/bin:" + os.environ['PATH']

Or if you always run the same scripts in the same order with the same arguments, wrap them in a shell-script which sets the environment for itself and runs those programs/scripts.

There is another workaround as well, as the last thing you do in the script that installs subversion, you could replace that process with another shell which has the environment you want. You'll be running a bash inside bash, but well, it could serve as a nice workaround.

#! /usr/bin/env python

# do stuff
# install subversion
# do more stuff

# at the point you exit
if everything_went_well:
  import os
  os.putenv('PATH', '/opt/subversion/bin:' + os.environ['PATH'])
  os.execv('/bin/bash', [])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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