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

Cameron Simpson cs at zip.com.au
Sat Jul 12 05:50:20 CEST 2014


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.

If you were using another terminal emulator it might be worth putting more work 
into getting stuff done after starting the emulator rather than before, but IMO 
gnome-terminal makes it too painful to bother.

Cheers,
Cameron Simpson <cs at zip.com.au>

Windows crashes:
===============
Cath   "Do the blue screens count?"
Jimbo  "According to the microsoft conference, they are not crashes, they are
         corrections"
John   "This was surely a joke as, at the time, the stock market crash during
         the week in question was being referred to as a 'correction' and not
         a crash"
Scott  "Yes, Windows is a joke"


More information about the Tutor mailing list