[Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.

Alan Gauld alan.gauld at btinternet.com
Fri Feb 26 03:25:44 EST 2016

On 26/02/16 04:49, Aneeque Khan wrote:
> The problem is that virtually any method of running a shell script
> will involve starting a separate process and setting the variables in
> that process' environment. So far as I can tell the only way to do
> what you want would be to run your shell script first then launch your
> python script from the shell script. That would mean breaking your
> current python script in two, and if you do a lot of work prior to
> launching the shell, that's probably not practical. 

Is it possible to adopt the approach described above?
Can you split your Python processing into two(or more)
parts, one before and one after the shell  script?
That will be the easiest way I suspect.

> I have recently started with python, can you direct me how we can achieve
> this by parsing the shell as a text file.

While parsing a shell script is not impossible, especially since you are
looking to trap the environment variables it's far from trivial.
Especially for
a beginner. I'd treat that as a last resort.

Also if the shell script does more than just set the variables its not
to work since you still need to run the script.

Do you have any control of the shell script content?
Or is that generated outside of your influence?
If you can control it you might be able to get the script to
dump it's environment vars to a temp file and read that.

The next question is what do you plan on doing with these once you
have them? They won't be of any value to the shell script or any other
script unless you set them again locally? Or are the just path locators?

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list