"Python" (External Evaluation System)

Details

- Python 3.4+ is supported.
- In most cases, the Wolfram Language will automatically provision one Python environment, and no manual user configuration is required. For advanced configuration, follow the instructions from the Configure Python for ExternalEvaluate workflow.
ExternalEvaluate Usage

- ExternalEvaluate["Python",code] executes Python code and returns the results as a Wolfram Language expression.
- ExternalEvaluate[{"Python","Evaluator"evaluator},code] executes the code using a Python environment specified by evaluator.
- The possible values for evaluator in ExternalEvaluate[{"Python","Evaluator"evaluator},…] include:
-
"path" the location of a Python executable File["path"] the location of a Python executable Association[…] a virtual environment specification - In ExternalEvaluate[{"Python","Evaluator"assoc},…], possible elements of assoc include:
-
"Dependencies" {} packages automatically installed using pip "EnvironmentName" Automatic name of the virtual environment directory to be created "PythonRuntime" Automatic specification of the Python runtime to be used, like "3.11" or File["/usr/bin/python3"] "BaseDirectory" Automatic the directory of the created virtual environment - Possible settings for "type" in ExternalEvaluate["Python""type",code] include:
-
"Expression" attempt to convert to a Wolfram Language expression "String" give the raw string output by the external evaluator "ExternalObject" return the result as ExternalObject
Data Types


- Not all Python built-in types are supported. The supported Python types are:
-
bool TrueFalse Boolean values array NumericArray arrays (from the NumPy library) bytearray ByteArray sequence of integers 0-255 bytes ByteArray immutable sequence of integers 0-255 complex Complex complex number date DateObject specific calendar date datetime DateObject specific date with time granularity Decimal Real arbitrary-precision real number dictionary Association association of keys and values float Real real-valued number Fraction Rational fraction object from the fractions module frozenset List unordered immutable set of unique values Image Image image (from Python PIL library) int Integer arbitrary-sized integer list List sequence of values long Integer arbitrary-sized integer None Null null value set List unordered set of unique values str String sequence of character values tuple List ordered sequence of values pyarrow.Table Tabular tabular with rectangular data - Any unsupported Python type that is callable returns an ExternalFunction; any other unsupported Python type returns an ExternalObject.
- The precision of a Real returned from a Decimal object is maintained when returned from ExternalEvaluate.
- Python complex types are required to be real, so any Complex expression returned from ExternalEvaluate will have real-valued components.
Supported External Operations
- ExternalOperation["Eval","code"] represents an external evaluation of "code".
- ExternalOperation["Eval","code",assoc] represents an external evaluation of "code" with parameters given by assoc.
- ExternalOperation["Call",func,arg1,arg2,…] calls the function func with the given arguments arg1, arg2, ….
- ExternalOperation["GetAttribute",obj,"attr"] gets the attribute "attr" of obj.
- ExternalOperation["SetAttribute",obj,"attr",val] sets the attribute "attr" of obj to the given value val.
- ExternalOperation["GetItem",obj,"item"] gets the item "item" of obj.
- ExternalOperation["SetItem",obj,"item",val] sets the item "item" of obj to the given value val.
- ExternalOperation["Import","path"] imports the module "path".
- ExternalOperation["Import","path","attr"] imports the module "path", then gets the attribute "attr" from the module.
- ExternalOperation["Cast",obj,"type"] casts obj to the given "type".
Examples
open allclose allBasic Examples (3)Summary of the most common use cases
Evaluate in Python and return the result:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-pcce2y

Create a list in Python and return the result:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-o81rq

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

Create a list with exponentials and logarithms:

Create a list with the days of the week:

Use the File wrapper to execute code contained in a file:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ibz33e


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-i9fhfq

Deploy code using CloudDeploy and then run the code directly from a CloudObject:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-qsnyet


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-6zggba

Use a URL wrapper to directly run code hosted online:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-yh45eq


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-o24t34

An Association can be used to automatically provision a Python virtual environment:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-j80han


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-iahv73

The Python virtual environment was created under $UserBaseDirectory:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-8cg7gl

Scope (51)Survey of the scope of standard use cases
Evaluate a Boolean statement in Python and return the result:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-detsey

Create a byte array in Python and return its equivalent in the Wolfram Language:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-fxthr2

Another way to work with a byte array in Python:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-e3tmu

Python list, tuple, set and frozenset are converted to List:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-mz97le


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-10wsw2


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-soov2w


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dlo641

Python dict are converted to Association:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dl94n0

When using a Python higher than 3.7, dictionary keys are ordered, just like Association:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-vlcok8

Create a Complex number in Python and return the result:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-vv5k

Fractions are automatically converted to Rational:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dds66q

Decimals are converted to Real:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-tw2o39

Fractions are automatically converted to Rational:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ek5ys0

Decimals are converted to Real:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-khgglc

Datetime objects are automatically converted:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-j692rn

NumPy arrays are automatically converted to NumericArray:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-pmz4ex

Pandas DataFrames are automatically converted to Dataset:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-68et0

PIL images are automatically converted to Image:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-lu5kv5

Generators are automatically converted to List:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-j0jozd

Any Python object that is iterable will be automatically converted to a List unless an explicit conversion method is implemented:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-u2tvwb

Create a session to work with pyarrow module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-7a4az6
Create a Tabular object from Python data:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-63et17

Use pyarrow to create Tabular from data of different shapes:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-1jk7dc


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-0ymsza

Pandas dataframes can be converted to Tabular objects by converting them to pyarrow first:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4vww5j


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-0y9gv6
String templates can be used to insert Wolfram Language expressions into Python code. Set two variables:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-g5j12f
The expression x^2+y^2 is evaluated in the Wolfram Language, and the result is converted and inserted into the Python code string:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-cy5wyt

Session Options (12)
"ReturnType" (3)
For the Python evaluation system, the default return type is "Expression":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-wtbdnk

Numbers, strings, lists and associations are automatically imported for the "Expression" return type:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-5qg8yf


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-7plv6t

The return type of "String" returns a string of the result by calling the Python function repr:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-blcv4


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-c4bwc0

"Evaluator" (5)
Evaluate Python code using a specified "Evaluator":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-eafuyy

An Association can be used to automatically provision a Python virtual environment:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jqhheo

"Dependencies" can be a list of strings that specify Python packages, using pip conventions to specify versions:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-eepzac

The File wrapper can also be used in order to specify "Dependencies" for an evaluator:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-bs1my9

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hl1zxe

Specify an "EnvironmentName" to customize the virtual environment folder name:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jaovon

Specify a "BaseDirectory" to change the installation directory:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-tm7tjc

"PythonRuntime" can be used to download a specific version:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-6stm5z

Use the File wrapper to use your own Python installation:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-yf6vsd

"SessionProlog" (1)
"SessionEpilog" (1)
"Prolog" (1)
Command Options (11)
"Command" (4)
When only a string of Python code is provided, the command is directly executed:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-c6bi9t

The above is equivalent to writing the command using this form:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-17i6vu
Use a File wrapper to run the code in a file:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-xxf9jq

The above is equivalent to writing the command using this form:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-rvukyg

Use a URL wrapper to directly run code hosted online:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-g13atf

The above is equivalent to writing the command using this form:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-u52xs5

Put code in a CloudObject:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hee6of

Evaluate directly from the cloud:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-9m0s4q
The above is equivalent to writing the command using this form:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-i5g92o
"ReturnType" (1)
By default, the command is executed using the "ReturnType" specified during the session creation:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-19xh84

Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-p4a30z

"Arguments" (2)
Use "Arguments" to call a Python function with arguments:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-6bkmpv

When a non-list argument is provided, a single argument is passed to the function:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-vczfev

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-eoly8d

You can define a function inside "Command" and directly call it with "Arguments":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-egsam4

The same result can be achieved by using a Rule:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-896a9d

You can also pass arguments by creating an ExternalFunction:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ea55zw


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-oxzo4e


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dourvn


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-9aymjh
"Constants" (1)
"TemplateArguments" (3)
When running a command, you can inline a TemplateExpression:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-fjfgam


You can explicitly fill TemplateSlot using "TemplateArguments":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-2px1xh

When a non-list argument is provided, a single template argument is passed to the template:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ir4foc

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ffga8d

You can name template slots and use an Association to pass named arguments to the template:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-5n2k7d

External Operations (16)
"Eval" (1)
Run an ExternalOperation that represents arbitrary code evaluation in Python:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-swuxd6

Use the second argument to pass an evaluation context:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-f36uo2

"Call" (3)
Define an ExternalOperation that creates a function in Python:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-9h6w6f

Define a function call by running the ExternalOperation "Call":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-rm5gdc

Run the external operation representing the function call using ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-tkrdu6

Any argument of the "Call" operation can be an ExternalOperation:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-bvf64x

Arguments can also be passed directly in ExternalEvaluate by doing the following:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-7rmkso

The result is equivalent of running the following Python code:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-knfcqv

Create an ExternalFunction for a Python function:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dc5lyn

Call the function by running the operation "Call":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-099awd

The same result can be achieved by doing the following:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-snh2r8

Create an ExternalObject for a Python function:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-gry43k


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4f4k15

Call the function by running the operation "Call":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-veyfut

The same result can be achieved by doing the following:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-1coimf

Alternatively, you can use ExternalObject subvalues:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-78xhno


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-e5v8ku
"GetAttribute" (1)
Start a Python session to work with dates:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-d7wsz4
Return an ExternalObject for a datetime object:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-zkk45a

Extract the year attribute by using "GetAttribute":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jfln28

The same can be done using an ExternalObject subvalue:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-1rthe2

For the Python evaluator, "GetAttribute" is the default operation, and ExternalOperation can be omitted:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-cotv78

The result is equivalent to running the following Python code:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-b1282w


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-u5uste
"SetAttribute" (1)
Start a Python session to work with decimal numbers:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-lf5zl4
Get the context for the decimal module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ds819o


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-602p3t
Check that the precision was set to 6:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4c6pmi


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-cbzrou

The result is equivalent to running the following Python code:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-by53sd

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-c3tha6
"GetItem" (2)

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4xlvm8
Create a Python dictionary and return it as an ExternalObject:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-nkldpq

Extract the "name" item by using "GetItem":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-zswued

The result is equivalent to running the following Python code:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-gb8un


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-d2klzy
Create an ExternalOperation that represents a dictionary:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-1xy53p

Create a new ExternalOperation to get the item "name":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-2298f9

Run the operation using ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-1hwbbz

"SetItem" (1)

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-fre8xk
Create an ExternalObject with a dictionary:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hsbioy


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-u2pg39
Check that the item was set to "joe":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-gqcjr7

Get the whole dictionary as an association, by using Expression:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-olk2xx


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-z9o0bw
"Import" (6)
Define an ExternalObject that imports the "datetime" module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-9nvzvj

Return the module as an ExternalObject:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-s0zngk

Define an ExternalObject that imports the date from the "datetime" module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-dek7oy

Call the operation with arguments using ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jfqhos

Define an ExternalObject that imports the "datetime" module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-onjl9m

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ollxlu

Use an ExternalFunction to call the method:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-t5npi4


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-rsyi3b
Define an ExternalObject that imports the fractions module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-8ffxyb

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-qrq3f0

Initialize the fraction by using ExternalObject subvalues:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-qmm8u0

The same can be done with the following syntax:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hpzx3o


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-3putwa
Define an "Import" operation that imports "ip_address":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-zuzo4

Create an address instance using ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jo4wyd

Use String as the return type to show the Python string version of the instance:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hxnsx8

Define an "Import" operation that imports "ip_address":

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4xoxng

Create an address instance using another ExternalOperation:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-w235o6

Execute the operation in ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-g2x673

"Cast" (1)
Create an ExternalObject that represents the current date:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-2ful7n

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-lh03sx

Use "Expression" to return the object as a Wolfram Language expression:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-k0ehns

The Cast operation can also run in ExternalObject subvalues:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-jeanm1

The symbol Expression is a shortcut for the same:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-pdqz05

Return the object as a string:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-gqmk1m

The symbol String is a shortcut for the same:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-yg8hmp

Return the object as an ExternalObject:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-yt1eew

The symbol ExternalObject is a shortcut for the same:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-xiafcd

The same can be achieved by using "ReturnType" in ExternalEvaluate:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-bnmhjm


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-wdx7xb
Applications (3)Sample problems that can be solved with this function
Expose the Python range function in Wolfram Language:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-qkn6dg

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-hyg6pe

Create a function that returns AST for a string of Python code using an AST module:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-3zt4x8

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-z6oplw

Use Pygments to create a function that does syntax highlighting of a block of code:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-741xjo

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-0mr6ej


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-2jeuuw


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-l3b4iv

Properties & Relations (1)Properties of the function, and connections to other functions
You can use to return expressions from Python:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-4oocv

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-yv0c2a

Expressions automatically evaluate after they are transferred in a kernel.

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-ebzl8t

Use Hold to control evaluation:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-tn30tp


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-h9smcn


https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-qkg1eo
Possible Issues (1)Common pitfalls and unexpected behavior
Generators are automatically converted to List:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-5c77jd

Returning an infinite generator will block your kernel forever:

https://wolfram.com/xid/0sy3vs7imm7sglw5nw6pzfagqp92g0i6-8f8ibz
