Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a Python program which will run a script in the Wolfram language and then return to the main program. Is it possible to return values/variables to the main program?

This is the code in Python running the Wolfram language code:

os.system('wolfram  -script file.wl')

Then in file.wl, for example, I can run a command such as 1+1, which will give me an output of 2.

Can I then use this value 2 in the rest of my Python program?

e.g. print (ans)
share|improve this question
    
you might be better off asking in a python forum, seems like to need something equivalent to ToExpression –  s0rce Dec 29 '13 at 1:10
    
Thanks for help, yes I just want the output from wolfram language accessible to python –  Hamoudy Dec 29 '13 at 1:12
    
does using Print on the output help? Also, please don't use random tags... this question is not related in anyway to wolfram-alpha-queries or mathematica-education –  rm -rf Dec 29 '13 at 1:20
    
This is on the raspberry pi and the wolfram command runs wolfram-language. The print outputs to the command window, but after the script runs, I want the variables accessible in python. –  Hamoudy Dec 29 '13 at 1:22
    
yes, but the tag you used is for Wolfram | Alpha (specifically, querying W|A from Mathematica) :) –  rm -rf Dec 29 '13 at 1:27
show 1 more comment

1 Answer

up vote 14 down vote accepted

The os.system command will not help you, because it does not return/provide the output of the command executed. What you want is to have a look at this answer on Stack Overflow to see how you can get the stdout of a command into a Python variable.

In your Mathematica script you simply Print the result and with the given method in the other answer you will fetch this result to your Python script.

Here a working example. I don't have a Raspberry Pi and therefore, the command is still good old MathematicaScript for me.

  1. Create a Mathematica script file which I called test.m here with the following content.

    Print[1+1]
    
  2. Create a runScript.py with the following content. Here I'm using the Python 2.7 function subprocess.check_output:

    import subprocess
    
    output = subprocess.check_output(['MathematicaScript', '-script', 'test.m'])
    print output
    
  3. Run the script with something like /usr/bin/python runScript.py.

share|improve this answer
    
Thank you for your response, but I wasn't sure how to implement it. output = Popen(["os.system", "wolfram -script gps.wl"], stdout=PIPE).communicate()[0] But then I get this error: NameError: name 'Popen' is not defined –  Hamoudy Dec 29 '13 at 1:52
    
@Hamoudy You need to full specify the namespace: subprocess.Popen and subprocess.PIPE etc. Look at the last code-block in this example! –  halirutan Dec 29 '13 at 1:55
    
I tried, but I get no output: prntscr.com/2erghu –  Hamoudy Dec 29 '13 at 2:05
    
Thanks a lot it worked!! –  Hamoudy Dec 29 '13 at 2:24
    
Yes you need the print otherwise nothing will be outputted –  Hamoudy Dec 29 '13 at 2:25
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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