1

I am new to Python (C++ fluent) and am learning on an as-need basis. I wrote a script that takes several arguments and creates and saves a matplotlib graph. It has no functions, methods, classes, etc. It is just a series of instructions that results in a graph. I would like to write another script that would execute this script with its arguments as simply as possible.

Is this possible in Python?

sgchako
  • 109
  • 1
  • 8
  • 1
    When posting a question checkout the search results SO puts below the title. The first match will do. http://stackoverflow.com/questions/89228/calling-an-external-command-in-python?rq=1 – MGP Apr 08 '13 at 20:03
  • 1
    It is unnecessary to run a subprocess to do this. Just clean up your code and add a function which will take the parameters – michaelmeyer Apr 08 '13 at 20:23

2 Answers2

2

Take a look at the stdlib's subprocess module: http://docs.python.org/2/library/subprocess.html

from subprocess import call
call([sys.executable, 'script.py', arg1, arg2])

For a complete list of your options take a look at this similar question: Calling an external command in Python

Read the docs on link I provided above, specially if you need this call to be secure (make sure you trust or validate those params).

UPDATE:

As an alternative (and better) option would be to run this code by just importing it.

If you clean it up and put it in a function and then import and call it from your main program you dont need to execute that module as a script and, if you need to, you could still be able to run it as standalone script easily:

# script.py
def func(param1, param2, param3)
    #...

if __name__=="__main__":
    # get params...
    func(param1, param2, param3)
    # handle output etc...


# main.py
# ...
from script import func
# ...
func(param1, param2, param3)
# ...
Community
  • 1
  • 1
gonz
  • 5,226
  • 5
  • 39
  • 54
  • @ gonz so if my first script is run on the command line like: `python ./script A B C` I would call "call" like: `call(['python ./script A B C'])` – sgchako Apr 08 '13 at 20:17
  • @user1535701: I've added an example in the answer that should work you. – gonz Apr 08 '13 at 20:48
  • You're probably better off using `call(['python', './script', 'A', 'B', 'C'])`, and using `sys.executable` instead of `python`. And you probably want the script to be in the same directory as the parent script, not in the current working directory. But otherwise… yeah, it should work. – abarnert Apr 08 '13 at 20:49
  • @abarnert the A, B, C I used in the example are actually command line arguments. example being, `call(['sys.executable', './script', 'sys.argv[3]', etc.])`. That said, should I still use single quotes to enclose those arguments that already come from the command line? – sgchako Apr 08 '13 at 20:56
  • @user1535701: No, don't put them in quotes. If you do, `script` will get the string `'sys.argv[3]` as its first argument, instead of whatever the value of `sys.argv[3]` is in the parent. (And that also means that if you want to use the one-big-string-and-shell-True solution, you have to use string formatting. But, as I said, I don't think you want to use that.) – abarnert Apr 08 '13 at 21:04
-1

You can use the subprocess module.

import subprocess

subprocess.Popen([sys.executable, "save_graph.py", parameter1, parameter2])

Upon execution, this code snippet will wait for the "save_graph.py" to fully complete execution before continuing.

If you want to spawn a child process that's detached from it's parent, you will have to do something like this:

DETACHED_PROCESS = 0x00000008
subprocess.Popen([sys.executable, "smtp_email.py", email, vcode], creationflags = DETACHED_PROCESS).pid
Bobby
  • 1,062
  • 9
  • 9
  • `Popen` does not wait for the child process to complete; it returns immediately. And your creationflags` is Windows-specific, and not actually documented to even work on Windows. – abarnert Apr 08 '13 at 20:48
  • @bobby what do you mean by sys.executable? will "python" work in its place? – sgchako Apr 08 '13 at 20:48
  • 1
    @user1535701: See [the docs](http://docs.python.org/2/library/sys.html#sys.executable). `sys.executable` runs the same version of Python that's running the current script, instead of whatever happens to be the first thing called `python` on your PATH (which may be the wrong Python, or may not exist, or …). – abarnert Apr 08 '13 at 20:51
  • @abarnert You're right, it does not wait for the child process to complete because it is detached from it's parent. This is useful when calling a script that could take a while to complete and whose return you don't care about. And yes, the creationflags is Windows specific but since the original question did not directly call for this tidbit, I didn't bother addressing other OSs. – Bobby Apr 09 '13 at 15:15
  • @BobbyLindsey: No, it's not because it's detached from its parent. "Detached" has a few different meanings (different on each platform) including process group leader, won't send `SIGCHLD`, without a controlling tty, without the parent's cmd.exe window, etc., but even if not a single one of them is true, `Popen` still does not wait. `Popen` _never_ waits. That's why the `Popen` object has a `wait` method. – abarnert Apr 09 '13 at 18:17