10
$\begingroup$

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)
$\endgroup$
6
  • $\begingroup$ you might be better off asking in a python forum, seems like to need something equivalent to ToExpression $\endgroup$
    – s0rce
    Commented Dec 29, 2013 at 1:10
  • $\begingroup$ Thanks for help, yes I just want the output from wolfram language accessible to python $\endgroup$
    – Hamoudy
    Commented Dec 29, 2013 at 1:12
  • $\begingroup$ 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 $\endgroup$
    – rm -rf
    Commented Dec 29, 2013 at 1:20
  • $\begingroup$ 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. $\endgroup$
    – Hamoudy
    Commented Dec 29, 2013 at 1:22
  • $\begingroup$ yes, but the tag you used is for Wolfram | Alpha (specifically, querying W|A from Mathematica) :) $\endgroup$
    – rm -rf
    Commented Dec 29, 2013 at 1:27

1 Answer 1

15
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$
    – Hamoudy
    Commented Dec 29, 2013 at 1:52
  • $\begingroup$ @Hamoudy You need to full specify the namespace: subprocess.Popen and subprocess.PIPE etc. Look at the last code-block in this example! $\endgroup$
    – halirutan
    Commented Dec 29, 2013 at 1:55
  • $\begingroup$ I tried, but I get no output: prntscr.com/2erghu $\endgroup$
    – Hamoudy
    Commented Dec 29, 2013 at 2:05
  • $\begingroup$ Yes you need the print otherwise nothing will be outputted $\endgroup$
    – Hamoudy
    Commented Dec 29, 2013 at 2:25
  • $\begingroup$ I tried it, but it shows nothing in the screen. Why? $\endgroup$
    – user11701
    Commented Jan 13, 2014 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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