Sapiens Search


How to run Mathematica functions on Python?

1-Create a script named runMath with the content:

    #!/usr/local/bin/MathematicaScript -script


    (*The next line prints the script name.*)


2-Give execution privilege to the file:
    sudo chmod +x runMath

3-Move the file to the execution path:

    sudo mv runMath /usr/bin/

4-Create a new script called run with the content:

    from subprocess import *
    from sys import *



5-Move to the execution path:

    sudo mv run /usr/bin

6-Finally, test it:

    $run Prime[100]

    $run 'Sum[2x-1,{x,1,k}]'

    $run Integrate[Log[x],x]
    -x + x*Log[x]

    $run 'Zeta[2]'

You can use with ou without '. The ' are needed to commands with spaces.

    $run 'f[n_] := f[n] = f[n - 1] + f[n - 2]; f[1] = f[2] = 1; Table[f[n],{n,5}]'
    {1, 1, 2, 3, 5}


  1. I followed the procedure exactly , but when tried to run it, lets say 'run Prime[100]' ,do not know why it is showing :

    bash: /usr/bin/run: Permission denied

    any help ?

  2. Hi string, your problem is simple, your script in /usr/bin/run isn't executable by other users. There are two ways, run as root ("sudo /usr/bin/run" ,for exemple or give it permissions, like "sudo chmod 777 /usr/bin/run", but this will give permissions to everyone execute). If you know your user you can do the following "sudo chown myUser:myUser /usr/bin/run && sudo chmod 711 /usr/bin/run". If you couldnt run it yet, please post a comment.

  3. Thank you so much, it worked ! I appreciate your help! your blog is full of useful information. :-)

  4. Instead of calling these functions and compute in my ubuntu terminal, is it possible to call the Mathematica functions directly to the Python shell (IDLE) and compute within it, in addition to some other parts of the code contains pure python syntex ?

  5. Yes sure. I will write a project on github with a working code and the post here to you. Please wait a little =)

  6. is it possible to call mathematica plot output in python. It would be great to do. Do you have any working solution of calling mathematica in Python.

  7. I tried above procedure, did nt work in ubuntu, i do not see:


    1. Hey jr, look for path like this :


    2. Take a look on a project called WITM (Web Interface to Mathematica) (using PHP) and MathKernel and MathematicaScript options. I don't remember now, but I think is possible, but maybe we need to do something WITM does but with Python. Take a look. =)

  8. This comment has been removed by the author.

  9. It has been what I am looking for. But still, I don't exactly follow the way to implement it. Could you explain in details?

    1) Is the runMath saved as .m format? Where should it be saved into?
    2) I can't run sudo chmod +x runMath, and sudo mv runMath /usr/bin/ in my Windows XP Command Prompt.
    3) Also, do we type runMath in verbatim? or should we modify into something like this:

    sudo chmod +x C:\Python27\runMath.m
    sudo mv C:\Python27\runMath.m /usr/bin/

    4) Is the script called 'run' also created using Mathematica or Python? If it is python, there is always a message error after entering 'parameter=argv[1]'.
    The message was

    Traceback (most recent call last):
    File "", line 1, in
    IndexError: list index out of range

    5) Also, where should the run script be saved into?

    6) To run Mathematica functions in Python, do I have to initiate or import anything beforehand?

    Thank you!!

    1. Hi gratitude38, let answer your questions^^

      1 - runMath is a Mathematica script, but we don't need save it as .m. You can name it just with 'runMath' if you like.

      2 - chmod +x is a unix command. In fact, this solution holds just on linux/unix systems... Take a look on the observation at the end.

      3 - This commands will not work to you if you run on Windows. Maybe you should take a look on Linux. Take a look on the observation at the end.

      4 - run is a script created on python language.

      5 - Take a look at the observation at the end.

      6 - Python need to import some things with these couple of lines:
      from subprocess import *
      from sys import *


      The truth, you just need to create a script runMath (wrote in Mathematica with the same content that I already wrote) and a script run (wrote in Python) like I already created too. But the important thing is the line 'command='/usr/bin/runMath' should point to your runMath script (wherever it is). For example: 'command='C:\runMath' (if you put it into C:). I don't know if we need something more to run on Windows systems, but write run, runMath and put it on the same folder is all you need to put it to work. The other things is just to let the things a little elegant for unix users. Help it hopes.

  10. Looking forward to reading more. Great blog post.Much thanks again. Great.