Bash – Mac OS X: Change $PATH from within python script

bashmac-osxpython

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

Best Answer

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