Message Boards Message Boards

3
|
54638 Views
|
13 Replies
|
6 Total Likes
View groups...
Share
Share this post:

What's the best way to call Mathematica from Python?

For some online courses, programming homework has to be submitted through a Python interface.  I'd like to be able to write the code in Mathematica and call it from Python.
POSTED BY: Frank Kampas
13 Replies
Posted 4 years ago
Take a look at this StackExchange thread

http://mathematica.stackexchange.com/questions/4643/how-to-use-mathematica-functions-in-python-programs

w
hich references this link

http://sapiensgarou.blogspot.com.br/2012/06/how-to-run-mathematica-functions-on.html

f
or an approach (which I have not tried ;-) as I am still at the "hello world" stage of learning Python...
POSTED BY: David Reiss
Sean, Could you post a simple example?  A couple lines of python and a couple lines of Mathematica?
I think that'd be a big help.
POSTED BY: Frank Kampas
1. I'm not sure what you mean here. You will have to write both Mathematica and Python code. The Python code will have to be in a .py file and the Mathematica code will be in a .m file. You will want to make sure your Mathematica code in the .m file works first and then you can use Python's call function to call it as you would if you were running it from the command line.

2. The notation "#!" is from Unix. Please see more about it here. It's not a windows thing really. I'm not sure how you run Python on your machine. If you're using Cygwin then maybe you can use it. Otherwise you should just write a python script and pass it to the python interpreter.

3. Reading the documentation on these functions may be the best way to really understand what these lines do. "$ScriptCommandLine[[2]]]" refers to the command line argument to the Mathematica script which it gives as a string. ToExpression converts the string into a valid Mathematica expression. After this you can use "value". In this case they give the example of printing out "value".

4. Yes math refers to the Mathematica kernel. The Mathematica kernel can take flags such as "-script" which indicate what the script to take is.

Please make sure you are familiar with writing both Python and Mathematica scripts independantly before trying to combine them. Once both are familiar, the instructions will make more sense.
POSTED BY: Sean Clarke
Posted 11 years ago
Hi guys! Thank you for posting about the way of calling Mathematica from Python. However, I am very new in building interprogram communication codes. 

May I request for a more detailed explanation on how to make it happen? My platform is Windows XP. 

First of all, in what kind of format should I save the codes? *.m or *.py? 

2) Is this line applicable to Windows platform as well?
#!/usr/bin/env python
Or shall I change it to where my python is installed?
#!/C:/Python27/python.exe

3) What do these two lines exactly mean? Does it mean that I should write my Mathematica codes here? 
e.g. 
value = ToExpression[ $ScriptCommandLine[[2]]]; Print[value ];
example from: http://mathematica.stackexchange.com/questions/4643/how-to-use-mathematica-functions-in-python-programs

# Create temporary file and write your Mathematica code to it

do_stuff()

4) Does the 'math' here mean MathKernel in Windows? Should I then write it in this way: 'C:\Program Files\Wolfram Research\Mathematica\9.0\MathKernel.exe'? What are, then, '-script' and the_file mean? What should I replace them with? 
call(['math', '-script', the_file])

Sorry, I know that I am as if asking too much, but I just don't know where I should be starting from in order to call Mathematica in Python.. The reason I need to know this badly is to utilize the more powerful Mathematica solvers in solving a large nonlinear system of equations which takes input from a third-party Python module called CoolProp. Could someone please assist me? Thank you in advance! 

P/s: Merry Christmas and Happy New Year 2014! emoticon 
POSTED BY: Foo Piew Tan
I am also for Simon's suggestion. I do not think there is official MathLink API for Python yet (I assume you were talking about call Mathematica function within a compile code like this)
POSTED BY: Shenghui Yang
I tried running python setup.py install in the Python directory and got a large number of unresolved external symbol error messages.  
POSTED BY: Frank Kampas
The python link isn't finished, tested, or documented. But instead of hiding it, the developers decided to let people find it and use it if they found it useful for them.

The only real way to know whether it will work for your purposes is to go ahead and try using it.
POSTED BY: Sean Clarke
I should have mentioned that I need to be able to pass data from Python to Mathematica..  
POSTED BY: Frank Kampas
Posted 12 years ago
You can access additional parameters passed to math.exe using the $CommandLine symbol.
POSTED BY: Eric Meyers
I would probably use the answer above.

Some people have created links between Python and Mathematica in the past. These don't seem to be maintained though and likely wouldn't be worth the trouble for just a homework assignment. Here's the example of one: http://library.wolfram.com/infocenter/MathSource/6622/

Lastly, there is an undocumented, 100% unsupported, may-or-may-not-work, use-at-your-own-risk, link in Mathematica's installation for python. I've used it before for a personal project and it seemed okay. You can find it in your installation directory under SystemFiles/Link/Python. In there you will see a small example and a short text file for an introduction. That's all there is about it available.
POSTED BY: Sean Clarke
Posted 12 years ago
Lastly, there is an undocumented, 100% unsupported, may-or-may-not-work, use-at-your-own-risk, link in Mathematica's installation for python. I've used it before for a personal project and it seemed okay. You can find it in your installation directory under SystemFiles/Link/Python. In there you will see a small example and a short text file for an introduction. That's all there is about it available.

The lack of support still seems utterly shocking to me. @Sean Clarke do you know why Wolfram would choose this? And are there any specific difficulties you had using the Python Link?
POSTED BY: haile
Posted 12 years ago
I guess the simplest would be to create a file with the Mathematica code and have math run it:

#!/usr/bin/env python
from subprocess import call

# Create temporary file and write your Mathematica code to it
do_stuff()

call(['math', '-script', the_file])

You can read more about math here: http://reference.wolfram.com/mathematica/ref/program/math.html
POSTED BY: Simon Schmidt
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract