10

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)
Peter Mortensen
  • 759
  • 4
  • 7
Hamoudy
  • 255
  • 2
  • 8
  • you might be better off asking in a python forum, seems like to need something equivalent to ToExpression – s0rce Dec 29 '13 at 01:10
  • Thanks for help, yes I just want the output from wolfram language accessible to python – Hamoudy Dec 29 '13 at 01:12
  • does using Print on the output help? Also, please don't use random tags... this question is not related in anyway to [tag:wolfram-alpha-queries] or [tag:mathematica-education] – rm -rf Dec 29 '13 at 01: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 01:22
  • yes, but the tag you used is for Wolfram | Alpha (specifically, querying W|A from Mathematica) :) – rm -rf Dec 29 '13 at 01:27
  • @Peter Mortensen Thank you for improving my question – Hamoudy Dec 30 '13 at 17:50

1 Answers1

15

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.

halirutan
  • 112,764
  • 7
  • 263
  • 474
  • 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 01: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 01:55
  • I tried, but I get no output: http://prntscr.com/2erghu – Hamoudy Dec 29 '13 at 02:05
  • Yes you need the print otherwise nothing will be outputted – Hamoudy Dec 29 '13 at 02:25
  • I tried it, but it shows nothing in the screen. Why? –  Jan 13 '14 at 06:46