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

Alan Gauld alan.gauld at btinternet.com
Sat Jul 12 10:02:42 CEST 2014


On 12/07/14 02:29, Jim Byrnes 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.

What I can't figure out is why you are trying to do this using 
gnome-terminal? (You may have explained and I've missed it, in which 
case I apologize) Why do you feel you need to start a terminal?

You normally execute the commands you want directly from within
Python not by driving a terminal emulator. Terminals are just
windows for humans. They run a shell that reads and executes
commands. They format the output to fit the window. So when
you use a terminal you are just adding 2 extra layers of
complexity.

You can change directory and run activate from within Python
without the terminal. You could even start a bash session if
you need to let the user (eg. you) type some commands. But you
shouldn't need to start a terminal and then try to force it
to execute commands. That just seems crazy to me. Or am I
missing something?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list