[Tutor] Command line scripts
ALAN GAULD
alan.gauld at btinternet.com
Sun Jan 9 15:03:36 CET 2011
> The following line is what I mean by calling a command line from within the
>app
> using subprocess.
>
> self.espeak = subprocess.Popen(['espeak', word],stdout =
> subprocess.PIPE).communicate()[0]
OK, Now I understand.
You want to call an external application from within your code
via subprocess. And you want to know if that will work ok in a
wxPython application?
> >> In other words, is a command line app different from
> >> bindings in a compiled app?
When you call an app using subprocess it spawns an entirely
separate process in the OS. There is no linkage to your process
apart from the redirection of stdin/stdout/stderr back to your
app instead of to a console.
> > So-called exe generators simply bundle the interpreter witrh
> > the code and auto run it.
>
> So it's basically just installing a sandbox version of python?
Sort of, its a minimalist version of python with only the modules
needed to run the app. And you can't run the interpreter on its
own you can only run the packaged app.
> Which ultimately is, if I have a standalone application, that doesn't
> come from a command line terminal launching(which I haven't gotten to
> yet), is using the command line calls going through subprocess going
> to be called without the terminal, or will it open a terminal
> automatically to make those calls?
The app (eSpeak in your example) will see your app as its "console".
That is, it will send all output to your app and read all input from your
app (so you need to make sure it gets any input it is expecting!)
The other thing to watch in a wxPython (or any other GUI framework)
is that if the app runs for a long time your app may "freeze" from
the user's perspective, so you may want to run it in the background
or as a separate thread in your app.
> Or will all of my python apps need to have a command line terminal
> open to launch them.
No, your app takes over the job of the terminal. When you launch
a program from the terninal the terminal app(for it is just an app like
any other) is doing the same as you, it is spawning a subprocess
that sends its output back to the teminal for it to display.
In fact you might find it a useful exercise to build a very basic terminal
app in wxPython first. Read commands from a command input field
and display the output in a text widget... Execute the commands
via subprocess. Its a good way to get used to using subprocess
and experimenting with its various options. If you are very keen
you can start adding command history and search etc to it too...
HTH,
Alan G.
More information about the Tutor
mailing list