Executing a python script with arguments from a python script

Discussion in 'Python' started by olaufr@gmail.com, Dec 12, 2005.

  1. Guest

    Hi,

    I need to call a python script, with command line arguments (it is an
    autonomous script with a __main__), from within another python script.
    Can I use exec() or execfile() for this? How to pass the arguments?

    Thanks,

    Olivier.
    , Dec 12, 2005
    #1
    1. Advertising

  2. James Stroud Guest

    wrote:
    > Hi,
    >
    > I need to call a python script, with command line arguments (it is an
    > autonomous script with a __main__), from within another python script.
    > Can I use exec() or execfile() for this? How to pass the arguments?
    >
    > Thanks,
    >
    > Olivier.
    >


    Obligatory "ugh."

    Are you sure you want to do that? Just make the part you want to execute into a
    function and then import it as a module. You will thank me later.

    To reluctantly answer your question: see os.system.

    But don't see os.system. Use python like python.

    James
    James Stroud, Dec 12, 2005
    #2
    1. Advertising

  3. Larry Bates Guest

    You can pass arguments into a python script, see getopt module.
    Then to call an external script you would use subsystem module
    (or os.system if you are on earlier version of python).

    If you can, just make the other python program into a
    function and import it as James Stroud suggests in a separate
    post.


    wrote:
    > Hi,
    >
    > I need to call a python script, with command line arguments (it is an
    > autonomous script with a __main__), from within another python script.
    > Can I use exec() or execfile() for this? How to pass the arguments?
    >
    > Thanks,
    >
    > Olivier.
    >
    Larry Bates, Dec 13, 2005
    #3
  4. James Stroud Guest

    Larry Bates wrote:
    > You can pass arguments into a python script, see getopt module.
    > Then to call an external script you would use subsystem module
    > (or os.system if you are on earlier version of python).


    I think getopt is a little dated. Try optparse. To quote the python
    documentation, it is a "More powerful command line option parser".

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

    James
    James Stroud, Dec 13, 2005
    #4
    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. Cédric Rossé
    Replies:
    1
    Views:
    520
    Cédric Rossé
    Nov 9, 2004
  2. sarmin kho
    Replies:
    1
    Views:
    365
    Cameron Laird
    Jun 4, 2004
  3. gaurav kashyap
    Replies:
    2
    Views:
    273
    gaurav kashyap
    Oct 16, 2008
  4. ashish
    Replies:
    4
    Views:
    287
    ashish
    Sep 20, 2012
  5. ashish
    Replies:
    5
    Views:
    504
    Piet van Oostrum
    Sep 20, 2012
Loading...

Share This Page