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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With