Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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().

share|improve this answer
    
Any way to emulate shell script "export" behavior for python script? –  Eye of Hell 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 :( –  Eye of Hell 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
add comment

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.

share|improve this answer
add comment

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', [])
share|improve this answer
add comment

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.