Help newbie with how to call a source command

Dave Angel davea at ieee.org
Thu Aug 13 15:48:55 EDT 2009


Joni Lee wrote:
> Hi all,
>  
> I'm landing here because I need some help that I couldn't get through it.
> Question 1. 
> I want to call a source command from python script. It will source some variables enviroment for further commands. I tried the following which did not work
> os.sys('source '+source_text)
> call(['source',source_text], shell=True)
> commands.getoutput('source '+source_text)
>  
> Anyone have experiences with this? Because I'm very new to this world so a details reply is really appreciated!!! thank you very much
>  
> Question 2.
> I call a shell command from python, this time it runs a script which runs some ruby scripts. And the error returns: "ruby: command not found"
>  
> Thank you again
>
>
>   

1)  You cannot do "source" from Python.  It's a shell command whose very 
nature is to remember some environment variables for the duration of 
that particular shell. So once it returns to your python program, 
they're lost.

However, if you just need those variables for invocations of the same 
shell, you have a couple of choices.  You can parse the file yourself, 
and set up an environment dictionary, and pass that when you invoke 
subsequent shells.

Or you can build a script on the fly, prepending whatever script you're 
planning to run with that source_text file.

2) In this question, and in general, you need to specify the Python 
version, the OS environment, the particular Python code  you used to 
launch the shell, the contents of the script, and the results you saw.  
Use copy and paste, don't just paraphrase.

And in a case like this one, somebody familiar with the environment 
(Ruby on Unix?) will probably have other questions:  environment 
dictionary passed to the shell, did you get a Ruby startup prompt?  Or 
whatever.

DaveA




More information about the Python-list mailing list