Redirecting Output from Mathematica Script to PIPE Instead of stdout Using subprocess.Popen Yields Null String

StackOverflow https://stackoverflow.com/questions/21588284

Question

I have a Mathematica script that I can run as a bash executable from Terminal. I want to run it from within Python and get the result out. This is the code I wanted to use:

proc = subprocess.Popen(["./solve.m", Mrefnorm, Mvert, Mcomp, Mangle],
        stdout=subprocess.PIPE,stderr=subprocess.PIPE)
result, err = proc.communicate()

Unfortunately the result is an empty string. However, when I run this code, the result is printed to the terminal window as I would expect:

proc = subprocess.Popen(["./solve.m", Mrefnorm, Mvert, Mcomp, Mangle],
        stdout=subprocess.sys.stdout,stderr=subprocess.sys.stdout)

I found this answer for someone with windows and it is the exact same problem I am having. Unfortunately his solution related to his firewall software sandboxing the processes. I already disabled mine to check if that would resolve it and it does not. I have tried everything that the commenters mentioned on his question with no success.

In summary, the Mathematica script runs in both cases (takes about 5 seconds for both) but when I use PIPE, I can't get at the output from the script.

Was it helpful?

Solution 2

If Mathematica doesn't like a redirected stdout then you could try to hoodwink it by providing a pseudo-tty:

import pipes
from pexpect import run # $ pip install pexpect

args = ["./solve.m", Mrefnorm, Mvert, Mcomp, Mangle]
command = " ".join(map(pipes.quote, args))
output, status = run(command, withexitstatus=True)

You could also use stdlib pty module directly to capture the output.

If you want to get separate stdout/stderr; you could try to workaround the bug mentioned by @Wayne Allen.

OTHER TIPS

Turns out there is a bug in Mathematica 9 with redirecting stdout. See https://mathematica.stackexchange.com/questions/20954/why-doesnt-my-script-work-when-i-redirect-stdout

I'm not sure why this is but I made it work like this:

 proc=subprocess.Popen('fullpath/math -initfile  fullpath/script.m' ,
        shell=True,
        stdout=subprocess.pipe )

For some reason the list form of the arg list does not work, and -script does not work.

Just checked you can pass extra arguments just fine adding to the string

 proc=subprocess.Popen('fullpath/math -initfile  fullpath/script.m arg1 arg2' ,
        shell=True,
        stdout=subprocess.pipe )

access the arguments in your script via $CommandLine

(mathematica 9, python 2.4.3, redhat )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top