Python Integration in Mathematica 12

Written by on May 5, 2019 in Mathematica, Programming, Python with 0 Comments

Mathematica has had Python support since ver 11.2 through ExternalEvaluate[]. In ver 11.3 it was possible to input Python expression in a cell by beginning with “>” character. The good news is that Mathematica 12 has significantly enhanced this integration.

Python Cells

Let us start with the simplest way to use Python code in Mathematica. When you type “>” as the first character in a cell, Mathematica automatically converts that cell into a Python “listener” (although Python is the default, you can also switch to NodeJS mode). See the following figure.

Python Cell

Python Cell

I hope you are able to see that the state is preserved across these cells. That is nice.

Working with Python this way is interesting, but probably not that useful because you are not able to mix Python with Mathematica.

Using ExternalEvaluate[]

In the simplest case, ExternalEvaluate[] lets us execute arbitrary Python code embedded in a string.

ExternalEvaluate Function

ExternalEvaluate Function

The first argument is the language string, in this case, “Python”, and the second argument is a string containing the external language expression to be evaluated.

Notice that in this case, each call to ExternalEvaluate[] executes in its own “session”, so there is no sharing of state across different cells (hence the error in the third expression). This mode is useful only if we are interested in “independent” evaluation of expressions, something that is not quite common.

Identifying External Evaluators

Before we proceed further, let us discuss the function FindExternalEvaluators[].  This function returns data about what external evaluators are available on the current platform. “Python” happens to be just one external system supported. NodeJS” is another system that is integrated into Mathematica 12. When I execute this function on my iMac (macOS 10.14.4), I get the following result:

Finding External Evaluators

Finding External Evaluators

By the way, for the Python integration to work properly, “pyzmq” package for Python must be installed on the current platform. See this. 

Working with Explicit Python Session

If we wish to create a Python session whose lifetime we want to control, then StartExternalSession[] function is what we need. This function sets up a session for the given external evaluator, and as you can guess, the ExternalEvaluate[] function we saw earlier takes this session as its first parameter. That means our evaluation is always in the context of this session object.

Starting a Python Session

Starting a Python Session

Here is how we can use this session:

Evaluation in a Session

Evaluation in a Session

 

The third expression above shows how we can invoke a specific function and pass arguments to it. You can also see that state is preserved across expressions.

The following is an example of a Python function taking two arguments:

Function with 2 Arguments

Function with 2 Arguments

Note the two ways of passing arguments to a function. Referencing specific functions like the above will be even more useful when we load a Python code file containing many definitions.

Function References

Function Reference

In the above example, I create a reference to the Python function “decr” and then use it as if it is a Mathematica function! Quite powerful indeed.

Another related function is “ExternalValue[]”. This function takes an external session and a symbol name, and returns the value of that symbol in that session.

Referencing a Value

Referencing a Value

Finally, when we are done with an external session, we have to free the session:

Closing a Session

Cleaning up a Session

In summary, I feel that Mathematica 12 has done a great job in enhancing the integration with external systems such as Python (NodeJS is also supported). I expect this external integration to include other languages and environments in the future.

You can download my Mathematica 12 notebook and the sample Python code file.

Have a nice weekend!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top