2
$\begingroup$

I want to run Python script with argument 'arg'

  > python app.py arg

Without 'arg' it works like

 session = StartExternalSession["Python"];
 ExternalEvaluate[session, File["app.py"]];

but I don't understend how to add 'arg'.

$\endgroup$
3
$\begingroup$

I have many many python environments on my computer and I was never able to get my ExternalEvaluator to recognize. My workaround is to just use a shell command. If your app just returns values, it may work well enough. For example, my rigid body physics answer was driven from Mathematica, but the heavy lifting was done with Blender's python. If your app is interactive, this answer is not for you.

If we copy the prog.py example from Python's argparse, then we can execute a simple shell command. Here is the python listing:

#!/usr/bin/env python
# prog.py
import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()

print(args.accumulate(args.integers))

Here is one way using Run:

SetDirectory[NotebookDirectory[]];
outputfile = CreateFile[];
Run["C:\\Users\\Tim\\Anaconda2\\envs\\env37\\python prog.py 1 2 3 4 \
--sum >>" <> outputfile];
stext = OpenRead[outputfile];
Read[stext, Number]
DeleteFile[outputfile];
(* 10 *)

Here is an alternative way using RunProcess:

SetDirectory[NotebookDirectory[]];
RunProcess[{"C:\\Users\\Tim\\Anaconda2\\envs\\env37\\python", 
   "prog.py", "--sum", "1", "2", "3", "4"}]["StandardOutput"]
(* "10" *)

Update in response to comment

The code in comment, Run["c:\\test\\python app.py --sum >> out2"] , returns an error because it is expecting a list of space delimited arguments. RunProcess gives more insight to the problem. For example:

SetDirectory[NotebookDirectory[]];
RunProcess[{"C:\\Users\\Tim\\Anaconda2\\envs\\env37\\python", 
  "prog.py", "--sum"}]
(* <|"ExitCode" -> 2, "StandardOutput" -> "", 
 "StandardError" -> "usage: prog.py [-h] [--sum] N [N ...]
  
  prog.py: error: the following arguments are required: N
  
  "|> *)

Exit code 0 means the code successfully executed. Non-zero exit code does not send the output to standard out, but to standard error. Standard error shows that there is a usage error requesting at least 1 numerical argument.

$\endgroup$
5
  • $\begingroup$ I try simply to run Run["c:\\test\\python app.py --sum >> out2"] and it doen't work. $\endgroup$ Oct 23 '20 at 7:07
  • $\begingroup$ @RodionStepanov I have an extended comment in the update. If you are using the same example, you need at least one numerical argument or it throws an error. RunProcess will give you more outputs as an association to help troubleshoot the issues. $\endgroup$
    – Tim Laska
    Oct 23 '20 at 17:10
  • $\begingroup$ I've got error RunProcess::pnfd: Program c:\test\python not found. Check Environment["PATH"] I understand that it is necessary to configure the environment. It's complicated. It would be nice go back to ExternalEvaluate [session, File ["app.py"]]; $\endgroup$ Oct 24 '20 at 8:31
  • 1
    $\begingroup$ @RodionStepanov If you provide a fully qualified path to python, you should not need to specify it in %PATH%. Does python launch in a command shell (cmd) if you type "c:\test\python"?. I was able to launch an ANSYS python that was not in the Path by providing the fully qualified path RunProcess[{"C:\\Program Files\\ANSYS \ Inc\\v201\\CEI\\bin\\cpython.bat", "prog.py", "--sum"}]. $\endgroup$
    – Tim Laska
    Oct 24 '20 at 19:14
  • $\begingroup$ If I type "c:\test\python" then python deas not lunch. But simply "python" works. So I run RunProcess[{"python", "app.py", "arg"}] and it works now! Thank you. $\endgroup$ Oct 25 '20 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.