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

Jim Byrnes jf_byrnes at comcast.net
Sat Jul 12 17:34:49 CEST 2014


On 07/12/2014 03:02 AM, Alan Gauld wrote:
> 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?
>

I guess because I don't have the depth of knowledge to know any better. 
I wanted to automate a tedious process using Python.  I was working with 
a Python3 version of breezypythongui on a Python2.7 system. So I would 
open a terminal, cd to the correct directory and run source 
bin/activate, which would leave me with an open window.  Then I could 
work with Python3 and see error messages. I just focused on doing the 
same thing, but using a script.

Regards,  Jim

Regards,  Jim



More information about the Tutor mailing list