* Mathematica* has had

*support since ver*

**Python***through*

**11.2***. In ver*

**ExternalEvaluate[]***it was possible to input*

**11.3***expression in a cell by beginning with*

**Python***character. The good news is that*

**“>”***has significantly enhanced this integration.*

**Mathematica 12****Python Cells**

Let us start with the simplest way to use * Python* code in

*. When you type*

**Mathematica***as the first character in a cell,*

**“>”***automatically converts that cell into a*

**Mathematica***(although*

**Python****“listener”***is the default, you can also switch to*

**Python***mode). See the following figure.*

**NodeJS**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

*with*

**Python***.*

**Mathematica****Using ExternalEvaluate[]**

In the simplest case, * ExternalEvaluate[]* lets us execute arbitrary

*code embedded in a string.*

**Python**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

*evaluation of expressions, something that is not quite common.*

**“independent”****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.

*happens to be just one external system supported.*

**“Python”***is another system that is integrated into*

**“****NodeJS”***. When I execute this function on my*

**Mathematica 12***(macOS 10.14.4), I get the following result:*

**iMac**By the way, for the * Python* integration to work properly,

*package for*

**“pyzmq”***must be installed on the current platform. See*

**Python***.*

**this****Working with Explicit Python Session**

If we wish to create a * Python* session whose lifetime we want to control, then

*function is what we need. This function sets up a session for the given external evaluator, and as you can guess, the*

**StartExternalSession[]***function we saw earlier takes this session as its first parameter. That means our evaluation is always in the context of this session object.*

**ExternalEvaluate[]**Here is how we can use this 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:

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.

In the above example, I create a reference to the * Python* function

*and then use it as if it is a*

**“decr”***function! Quite powerful indeed.*

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

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

In summary, I feel that * Mathematica 12* has done a great job in enhancing the integration with external systems such as

*(*

**Python***is also supported). I expect this external integration to include other languages and environments in the future.*

**NodeJS**You can download my * Mathematica 12 notebook* and the sample

*file.*

**Python****code**Have a nice weekend!

## Recent Comments