PythonExpression
- Import and Export support PythonExpression.
- There is no associated file extension for PythonExpression. To execute ".py" files, use ExternalEvaluate.
- Python must be installed and configured on your system as described in Configure Python for ExternalEvaluate.
Background & Context

-
- Code representation format for the Python programming language.
- PythonExpression supports Python numbers, strings, lists, Booleans and dictionaries.
- Plain text format.
- Python was first released as a programming language in 1991.
Import & Export

- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Import Elements

- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - Details of elements...
-
"Data" the literal Python expression - ImportString uses the "Data" element by default.
- The symbol Null is represented as the Python value "None".
- Not all Python built-in types are supported. The supported Python types are:
-
tuple List an ordered sequence of values dictionary Association an association of keys and values int Integer arbitrary-sized integer long Integer arbitrary-sized integer float Real real-valued number complex Complex a complex number list List a sequence of values boolean TrueFalse Boolean values string String a sequence of character values None Null null value
Examples
open allclose allBasic Examples (6)Summary of the most common use cases
In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-k2f7ic
Out[1]=1

In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-dgdvu4
Out[1]=1

In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-no2gbc
Out[1]=1

Import a dictionary as a Wolfram Language list:
In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-fd4kbr
Out[1]=1

Export an Association as a Python dictionary:
In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-8xj45j
Out[1]=1

Import the result of evaluating external code in Python:
In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-02leg1
Out[1]=1

Confirm it is the same as using the "ReturnType" of "Expression":
In[2]:=2

✖
https://wolfram.com/xid/0idaamy30cv3rma-ev9g28
Out[2]=2

Possible Issues (1)Common pitfalls and unexpected behavior
"PythonExpression" internally starts and stops a Python session every time ImportString is called, which takes a substantial amount of time:
In[1]:=1

✖
https://wolfram.com/xid/0idaamy30cv3rma-wqb9ys
In[2]:=2

✖
https://wolfram.com/xid/0idaamy30cv3rma-wn8z13
Out[2]=2

When performance is a concern, manually manage a Python session using ExternalEvaluate:
In[3]:=3

✖
https://wolfram.com/xid/0idaamy30cv3rma-e7mwsk
Out[3]=3
