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

**URL:** <https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585>\
**Category:** Questions\
**Tags:** mathematica, external-interfaces\
**Created:** [September 19, 2013, 10:12pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585 "2013-09-19T22:12:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [September 19, 2013, 10:12pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/1 "2013-09-19T22:12:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![schmidtsimon](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schmidtsimon](https://community.wolfram.com/u/schmidtsimon)\
**Post date:** [September 20, 2013, 12:28am UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/2 "2013-09-20T00:28:55Z")

</div>

I guess the simplest would be to create a file with the Mathematica code and have math run it:

```auto
#!/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](http://reference.wolfram.com/mathematica/ref/program/math.html)

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [September 20, 2013, 2:18am UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/3 "2013-09-20T02:18:40Z")

</div>

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/  
 ](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.

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [September 20, 2013, 1:18pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/4 "2013-09-20T13:18:03Z")

</div>

I should have mentioned that I need to be able to pass data from Python to Mathematica..

---

<div class="post-metadata">

**Author:** ![haile](https://avatars.discourse-cdn.com/v4/letter/h/848f3c/32.png) [@haile](https://community.wolfram.com/u/haile)\
**Post date:** [October 2, 2013, 5:26am UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/5 "2013-10-02T05:26:36Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [October 2, 2013, 6:52pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/6 "2013-10-02T18:52:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [October 2, 2013, 7:50pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/7 "2013-10-02T19:50:02Z")

</div>

I tried running python setup.py install in the Python directory and got a large number of unresolved external symbol error messages.

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [October 2, 2013, 9:13pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/8 "2013-10-02T21:13:31Z")

</div>

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](http://reference.wolfram.com/mathematica/tutorial/RunningMathematicaFromWithinAnExternalProgram.html))

---

<div class="post-metadata">

**Author:** ![etm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/etm/32/8360_2.png) [@etm](https://community.wolfram.com/u/etm)\
**Post date:** [October 10, 2013, 7:13pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/9 "2013-10-10T19:13:28Z")

</div>

You can access additional parameters passed to math.exe using the $CommandLine symbol.

---

<div class="post-metadata">

**Author:** ![tanfp](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@tanfp](https://community.wolfram.com/u/tanfp)\
**Post date:** [December 19, 2013, 12:50pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/10 "2013-12-19T12:50:19Z")

</div>

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?

1. 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**

1. 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](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()\*\*

1. 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! 🙂

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [December 19, 2013, 10:40pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/11 "2013-12-19T22:40:51Z")

</div>

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](http://en.wikipedia.org/wiki/Shebang_%28Unix%29). 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](http://reference.wolfram.com/mathematica/tutorial/MathematicaScripts.html) independantly before trying to combine them. Once both are familiar, the instructions will make more sense.

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [December 20, 2013, 1:30pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/12 "2013-12-20T13:30:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [January 10, 2014, 8:51pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/13 "2014-01-10T20:51:45Z")

</div>

Take a look at this StackExchange thread

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

w]([http://mathematica.stackexchange.com/questions/4643/how-to-use-mathematica-functions-in-python-programs](http://mathematica.stackexchange.com/questions/4643/how-to-use-mathematica-functions-in-python-programs))hich references this link

[[Sapiens Garou: How to run Mathematica functions on Python?](http://sapiensgarou.blogspot.com.br/2012/06/how-to-run-mathematica-functions-on.html)

f]([Sapiens Garou: How to run Mathematica functions on Python?](http://sapiensgarou.blogspot.com.br/2012/06/how-to-run-mathematica-functions-on.html))or an approach (which I have not tried 😉 as I am still at the “hello world” stage of learning Python…

---

<div class="post-metadata">

**Author:** ![stergbaird](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@stergbaird](https://community.wolfram.com/u/stergbaird)\
**Post date:** [March 20, 2021, 5:40pm UTC](https://community.wolfram.com/t/whats-the-best-way-to-call-mathematica-from-python/585/14 "2021-03-20T17:40:41Z")

</div>

> **[Wolfram Client Library for Python — Wolfram Client Library for Python 1.4.1...](https://reference.wolfram.com/language/WolframClientForPython/)**
