Calling external program from within python

Discussion in 'Python' started by Emmanouil Angelakis, Jul 25, 2008.

  1. Hi,

    I am tryiong to do something obviously trivial such as:
    I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.

    How do I do that?

    thanks i advance!
    manolis
     
    Emmanouil Angelakis, Jul 25, 2008
    #1
    1. Advertising

  2. On Jul 25, 7:56 am, Emmanouil Angelakis <>
    wrote:
    > Hi,
    >
    > I am tryiong to do something obviously trivial such as:
    > I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the  "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.
    >
    > How do I do that?
    >
    > thanks i advance!
    > manolis


    There are probably many ways to do this. I would recommend checking
    out the subprocess module and see if it does what you want. Or you
    could learn a little Tkinter or wxPython and use that to get the
    user's variable. Or you could even do it via the command line using
    the "raw_input" command.

    http://docs.python.org/lib/module-subprocess.html

    Mike
     
    Mike Driscoll, Jul 25, 2008
    #2
    1. Advertising

  3. Mike Driscoll schrieb:
    > On Jul 25, 7:56 am, Emmanouil Angelakis <>
    > wrote:
    >> Hi,
    >>
    >> I am tryiong to do something obviously trivial such as:
    >> I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.
    >>
    >> How do I do that?
    >>
    >> thanks i advance!
    >> manolis

    >
    > There are probably many ways to do this. I would recommend checking
    > out the subprocess module and see if it does what you want.


    This will only work if the program can be fully controlled by
    commandline arguments. If interaction is required, the OP might consider
    using pexpect.


    > Or you
    > could learn a little Tkinter or wxPython and use that to get the
    > user's variable. Or you could even do it via the command line using
    > the "raw_input" command.


    I fail to see how *gathering* input (regardless of the method) solves
    the problem of *passing* input to a subprocess.

    Diez
     
    Diez B. Roggisch, Jul 25, 2008
    #3
  4. Grant Edwards schrieb:
    > On 2008-07-25, Diez B. Roggisch <> wrote:
    >
    >>> There are probably many ways to do this. I would recommend
    >>> checking out the subprocess module and see if it does what you
    >>> want.

    >> This will only work if the program can be fully controlled by
    >> commandline arguments.

    >
    > Why do you say that? You can interact with programs via
    > stdin/stdout using the subprocess module.



    Because usually if a program *prompts* the user to enter input (and that
    was what I read from the OP's post), one has to deal with pseudo
    terminals, not with stdin/out.

    >> If interaction is required, the OP might consider using
    >> pexpect.

    >
    > Pexpect is a good option, but it's just an automation layer on
    > top of the same facilities that subprocess provides.


    AFAIK it's more than that. I'm not an expert on pseudo terminals, but
    AFAIK setting using module pty isn't possible using subprocess.

    Diez
     
    Diez B. Roggisch, Jul 25, 2008
    #4
  5. Emmanouil Angelakis

    oj Guest

    On Jul 25, 3:44 pm, "Diez B. Roggisch" <> wrote:
    > Because usually if a program *prompts* the user to enter input (and that
    > was what I read from the OP's post), one has to deal with pseudo
    > terminals, not with stdin/out.


    How does the program writing some text before taking input change how
    it takes input?

    If it runs in a terminal and takes input from the user via keyboard,
    as in the user types a response and presses enter, it's most likely
    using stdin, in which case subprocess would be perfect.

    Perhaps that OP can clarify how the tcal program takes input?
     
    oj, Jul 25, 2008
    #5
  6. oj schrieb:
    > On Jul 25, 3:44 pm, "Diez B. Roggisch" <> wrote:
    >> Because usually if a program *prompts* the user to enter input (and that
    >> was what I read from the OP's post), one has to deal with pseudo
    >> terminals, not with stdin/out.

    >
    > How does the program writing some text before taking input change how
    > it takes input?
    >
    > If it runs in a terminal and takes input from the user via keyboard,
    > as in the user types a response and presses enter, it's most likely
    > using stdin, in which case subprocess would be perfect.


    Try using SSH with subprocess and prepare to be disappointed when it
    asks for a password.

    See this for a discussion:

    http://mail.python.org/pipermail/distutils-sig/2007-May/007553.html


    Diez
     
    Diez B. Roggisch, Jul 25, 2008
    #6
  7. On Fri, 25 Jul 2008 08:13:55 -0700, oj wrote:

    > On Jul 25, 3:44 pm, "Diez B. Roggisch" <> wrote:
    >> Because usually if a program *prompts* the user to enter input (and that
    >> was what I read from the OP's post), one has to deal with pseudo
    >> terminals, not with stdin/out.

    >
    > How does the program writing some text before taking input change how
    > it takes input?


    It might not write the text before taking input because it is buffered.
    But if the controlling program waits for some text before sending input to
    the other program all hangs.

    > If it runs in a terminal and takes input from the user via keyboard,
    > as in the user types a response and presses enter, it's most likely
    > using stdin, in which case subprocess would be perfect.


    But buffering messes with the order of inputs and outputs. Buffering is
    different if you "talk" to a real (or pseudo) terminal or to a file or
    pipe and the calling program can't influence the called programs buffering
    except if it pretends to be a terminal, which pexpect does.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jul 25, 2008
    #7
  8. On Jul 25, 9:28 am, "Diez B. Roggisch" <> wrote:
    > Mike Driscoll schrieb:
    >
    > > On Jul 25, 7:56 am, Emmanouil Angelakis <>
    > > wrote:
    > >> Hi,

    >
    > >> I am tryiong to do something obviously trivial such as:
    > >> I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the  "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.

    >
    > >> How do I do that?

    >
    > >> thanks i advance!
    > >> manolis

    >
    > > There are probably many ways to do this. I would recommend checking
    > > out the subprocess module and see if it does what you want.

    >
    > This will only work if the program can be fully controlled by
    > commandline arguments. If interaction is required, the OP might consider
    > using pexpect.
    >
    > > Or you
    > > could learn a little Tkinter or wxPython and use that to get the
    > > user's variable. Or you could even do it via the command line using
    > > the "raw_input" command.

    >
    > I fail to see how *gathering* input (regardless of the method) solves
    > the problem of *passing* input to a subprocess.
    >
    > Diez


    My understanding of the OP's request was that "tsys2list" was a custom
    script of some sort that gathered info from the user, so I wanted to
    know why he didn't just skip calling it and write something in Tkinter
    or wxPython or even via the CLI instead.

    I'm probably just not understanding something.

    Mike
     
    Mike Driscoll, Jul 25, 2008
    #8
  9. Mike Driscoll schrieb:
    > On Jul 25, 9:28 am, "Diez B. Roggisch" <> wrote:
    >> Mike Driscoll schrieb:
    >>
    >>> On Jul 25, 7:56 am, Emmanouil Angelakis <>
    >>> wrote:
    >>>> Hi,
    >>>> I am tryiong to do something obviously trivial such as:
    >>>> I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.
    >>>> How do I do that?
    >>>> thanks i advance!
    >>>> manolis
    >>> There are probably many ways to do this. I would recommend checking
    >>> out the subprocess module and see if it does what you want.

    >> This will only work if the program can be fully controlled by
    >> commandline arguments. If interaction is required, the OP might consider
    >> using pexpect.
    >>
    >>> Or you
    >>> could learn a little Tkinter or wxPython and use that to get the
    >>> user's variable. Or you could even do it via the command line using
    >>> the "raw_input" command.

    >> I fail to see how *gathering* input (regardless of the method) solves
    >> the problem of *passing* input to a subprocess.
    >>
    >> Diez

    >
    > My understanding of the OP's request was that "tsys2list" was a custom
    > script of some sort that gathered info from the user, so I wanted to
    > know why he didn't just skip calling it and write something in Tkinter
    > or wxPython or even via the CLI instead.
    >
    > I'm probably just not understanding something.


    The program is doing some work, and needs to be told what actually to
    do. It does so by asking the user, sure. But then it *works*. I think
    that's pretty clear from

    """
    I have a c program called "tsys2list" that when it is ran it asks the
    user to give the value of "tcal" which is a variable. I want to call the
    "tsys2list" from within a pyrthon script lets call it "gamma.py"
    >>>but<<< I want to pass the value of "tcal" to th eprogram

    automatically and not by interacting with the keyboard.
    """

    So replacing it with something that asks the same questions leaves us
    with the work undone...


    Diez
     
    Diez B. Roggisch, Jul 25, 2008
    #9
  10. These answers are too elaborate and abstract for the question.

    Emmanouil,

    Here is a program "myprog" which takes input and writes output to a
    file. It happens to be python but it could be anything.

    #####
    #!/usr/bin/env python
    a = int(raw_input("enter thing 1 "))
    b = int(raw_input("enter thing 2 "))
    c = raw_input("enter output file name ")
    f = open(c,"w")
    f.write("answer is %s\n" % str(a + b))
    f.close()
    #####

    Here is python code which runs that program

    #####
    import os

    f = os.popen("/path/to/myprog","w")
    f.write("3\n4\noutputname\n") #can also do three separate writes if
    that is more convenient
    f.close()
    #####

    For some reason os.popen is deprecated in favor of the more verbose
    subprocess.Popen, but this will work for a while.

    Here is an even simpler approach that will take you a long way. This
    should be very easy to understand.

    #####
    import os

    params = open("temp","w")
    params.write("3\n")
    params.write("4\n")
    params.write("outputname2\n")
    params.close()

    os.system("/path/to/myprog < temp")
    #####

    If you want python to pick up the stdout of your program as well look
    into popen2.

    Try basing your solution on those and if you have any problems let us
    know what they are. In most cases such as you describe these will
    work.

    mt
     
    Michael Tobis, Jul 25, 2008
    #10
  11. Emmanouil Angelakis

    Terry Reedy Guest

    Michael Tobis wrote:

    > For some reason os.popen is deprecated in favor of the more verbose
    > subprocess.Popen, but this will work for a while.


    As explained in
    http://www.python.org/dev/peps/pep-0324/
    subprocess consolidated replaced several modules and functions (popen*,
    system, spawn*, ???)with better security, exception handling, and
    flexibility. The deprecated modules are gone in 3.0, so the OP might
    want to start with subprocess now.
     
    Terry Reedy, Jul 25, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Slaughter

    Calling external program in C++

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    9
    Views:
    25,834
  2. Juggler

    calling external program from within C++

    Juggler, Feb 24, 2005, in forum: C Programming
    Replies:
    5
    Views:
    583
    Nils Weller
    Feb 24, 2005
  3. Jeff
    Replies:
    13
    Views:
    1,149
    vanilla
    Oct 6, 2007
  4. Replies:
    7
    Views:
    1,355
  5. Mellow Crow
    Replies:
    6
    Views:
    420
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page