"Python" (External Evaluation System)


ExternalEvaluate Usage

  • ExternalEvaluate["Python",code] executes a code string in a Python REPL and returns the results as a Wolfram Language expression.
  • ExternalEvaluate["Python""String",code] executes the code string in a Python REPL and does not interpret the results.

Data Types

  • Not all Python built-in types are supported. The supported Python types are:
  • boolTrue|FalseBoolean values
    arrayNumericArrayarrays (from the NumPy library)
    bytearrayByteArraysequence of integers 0-255
    bytesByteArrayimmutable sequence of integers 0-255
    complexComplexcomplex number
    dateDateObjectspecific calendar date
    datetimeDateObjectspecific date with time granularity
    DecimalRealarbitrary-precision real number
    dictionaryAssociationassociation of keys and values
    floatRealreal-valued number
    FractionRationalfraction object from the fractions module
    frozensetListunordered immutable set of unique values
    ImageImageimage (from Python PIL library)
    intIntegerarbitrary-sized integer
    listListsequence of values
    longIntegerarbitrary-sized integer
    NoneNullnull value
    setListunordered set of unique values
    strStringsequence of character values
    tupleListordered sequence of values
  • The precision of a Real returned from a Decimal object is maintained when returned from ExternalEvaluate.
  • Python complex types are required to be real, and so any Complex expression returned from ExternalEvaluate will have real-valued components.

Usage Notes

  • String templates (<**>) can be used to evaluate and insert Wolfram Language expressions into the Python code string.


open all close all

Basic Examples  (1)

Evaluate in Python and return the result:

Click for copyable input

Create a list in Python and return the result:

Click for copyable input

Type > to get a Python code cell that uses ExternalEvaluate to evaluate:

import math [math.sqrt(i) for i in range(10)]

Create a list with exponentials and logarithms:

[[i,math.exp(i),math.log(i)] for i in range(1,10)]

Create a list with the days of the week:

import calendar [calendar.day_name[i] for i in range(7)]

Scope  (2)