[Tutor] How can I open and use gnome-terminal from a Python script?

Jim Byrnes jf_byrnes at comcast.net
Sat Jul 12 17:22:19 CEST 2014


On 07/11/2014 10:50 PM, Cameron Simpson wrote:
> On 11Jul2014 20:29, Jim Byrnes <jf_byrnes at comcast.net> wrote:
>> I've worked on this a little more. If I create a file like:
>>
>> #!/usr/bin/python
>> import os, subprocess
>> subprocess.Popen(args=["gnome-terminal",
>>    "--working-directory=/home/jfb/Documents/Prog/Python/breezygui"])
>>
>> and execute it, it will take me to the correct directory.  Once there
>> if I type in 'source bin/activate' I will get a virtualenv. However I
>> can't figure out how to do it from my script.
>>
>> I have tried adding "--command=source bin/active" to args=
>> but then I get this error:
>>
>> There was an error creating the child process for this terminal
>> Failed to execute child process "source" (No such file or directory)
>>
>> To check if I could even use "--command=", I added "--command=python"
>> and I got a  python session.
>>
>> Could some one tell me what I need to do to issue the command to setup
>> virtualenv?
>
> Your problem is that "source" is a shell builtin because it must affect
> the shell internals, and although gnome-terminal's --command option
> takes a string it does not seem to be a shell string, passed to "sh".
> Instead, it seems gnome-terminal takes it upon itself to take a string
> and break it up into words and expected the first word to be an
> executable program i.e. "source" in your case.
>
> Suggestions below, but first a tiny rant on the side: gnome-terminal's
> command specification option is rubbish, a long standing gripe of mine
> with gnome-terminal. Most decent terminal emulators take a -e option and
> follow command strings (just like you're passing to subprocess.Popen).
> Some are less helpful (eg OSX Terminal) and accept only a shell command;
> in Terminal's case it seems to be literally typed at the terminal :-(
> gnome-terminal seems to do neither.
>
> Returning to your task:
>
> Virtualenv is a directory to hold python modules etc and some "activate"
> scripts to set up the environment so that this is used by commands.
>
> People are generally pointed at the "bin/activate" shell file to source
> to set things up, but that doesn't need to happen _inside_ the terminal.
> You can do it outside and then run the terminal.
>
> An example shell command might look like this:
>
>    cd /home/jfb/Documents/Prog/Python/breezygui
>    . ./bin/activate
>    gnome-terminal
>
> or
>
>    cd /home/jfb/Documents/Prog/Python/breezygui; . ./bin/activate; exec
> gnome-terminal
>
> which avoids the difficulties with gnome-terminal's command line options.
>
> So you could adapt your Popen invocation to look like this:
>
>    subprocess.Popen(args=["sh", "-c", "cd
> /home/jfb/Documents/Prog/Python/breezygui; . ./bin/activate;
> gnome-terminal"])
>
> That is only one line, in case some mail program breaks it up.

Thank you, that worked.  At first I thought it was not working because 
when I did it manually I ended up with a prompt that looked like:

(breezygui)jfb at jims1204:~/Documents/Prog/Python/breezygui$

The script result did not have the (breezygui) at the front.  Luckily I 
decided to test it and when I type python I got python3.

Thanks,  Jim





More information about the Tutor mailing list