-
See Also
- ExternalEvaluate
- ExternalObject
- ExternalOperation
- StartExternalSession
- APIFunction
- FunctionCompile
- CloudFunction
- Function
-
- Formats
- JavaScriptExpression
- PythonExpression
-
- External Evaluation Systems
- Python
- NodeJS
- Julia
- Java
- Octave
- Shell
- Ruby
- R
- Jupyter
- SQL
- SQL-JDBC
- CUDA
- Related Guides
- Workflows
-
-
See Also
- ExternalEvaluate
- ExternalObject
- ExternalOperation
- StartExternalSession
- APIFunction
- FunctionCompile
- CloudFunction
- Function
-
- Formats
- JavaScriptExpression
- PythonExpression
-
- External Evaluation Systems
- Python
- NodeJS
- Julia
- Java
- Octave
- Shell
- Ruby
- R
- Jupyter
- SQL
- SQL-JDBC
- CUDA
- Related Guides
- Workflows
-
See Also
ExternalFunction[sys,"f"]
represents an external function named "f" defined in the external evaluator sys.
ExternalFunction[session,"f"]
represents an external function "f" in the specified ExternalSessionObject.
ExternalFunction[sys,"code"]
represents an external function defined by the code fragment "code".
ExternalFunction[obj,"method"]
represents a method bound to the ExternalObject.
ExternalFunction
Listing of External Evaluators »ExternalFunction[sys,"f"]
represents an external function named "f" defined in the external evaluator sys.
ExternalFunction[session,"f"]
represents an external function "f" in the specified ExternalSessionObject.
ExternalFunction[sys,"code"]
represents an external function defined by the code fragment "code".
ExternalFunction[obj,"method"]
represents a method bound to the ExternalObject.
Details
- In ExternalFunction[sys,"code"], "code" is typically a pure or lambda function in the external system.
- In ExternalFunction[sys, …], possible choices for sys include:
-
"Python" Python "NodeJS" JavaScript running through Node.js "Julia" Julia "Ruby" Ruby "R" R "Jupyter" Jupyter kernel
Examples
open all close allBasic Examples (3)
Create an external function using the absolute value function in Python:
abs = ExternalFunction["Python", "abs"]Compute the absolute value of
with the external Python function:
abs[-2.7]session = StartExternalSession["Python"]addtwo = ExternalFunction[session, "def addtwo(x,y):
return x+y"
]addtwo[4, 5]DeleteObject[session]session = StartExternalSession["Python"]Return an object for a Python module:
module = ExternalEvaluate[session, ExternalOperation["Import", "datetime"]]Run a method from the module, using ExternalFunction:
ExternalFunction[module, "date"][2012, 10, 10]DeleteObject[session]Scope (6)
Define a lambda function in Python:
times = ExternalFunction["Python", "lambda x,y: x * y"]Use this Python function in the Wolfram Language:
times[2, 3]Use a Python built-in function:
list = ExternalFunction["Python", "list"]Use the external function to convert a string to a list:
list["hello"]session = StartExternalSession["Python"]Define a function using complex numbers:
ExternalEvaluate[session, "from cmath import phase
def arg(re,im):
c=complex(re,im)
return phase(c)"]Compute the argument of a complex number via the Python phase function:
z = 1.2 + 3.4I;
ExternalFunction[session, "arg"][Re[z], Im[z]]DeleteObject[session]nodejs = StartExternalSession["NodeJS"]In NodeJS, it is not enough to just define a function—you have to explicitly return the function for ExternalFunction to recognize it as a callable function:
incr = ExternalFunction[nodejs, "function increment(x) {return x+1}; increment"]incr[3]DeleteObject[nodejs]julia = StartExternalSession["Julia"]addFive = ExternalFunction[julia, "f(x) = x + 5"]addFive[10]DeleteObject[julia]ExternalFunction supports the same command specification as ExternalEvaluate:
func = ExternalFunction["Python", <|"Command" -> "range", "ReturnType" -> "String"|>]func[2]Properties & Relations (5)
ExternalOperation can be used to extract the operation from an ExternalFunction:
op = ExternalOperation[ExternalFunction["Python", "max"]]Run the operation using ExternalEvaluate:
ExternalEvaluate["Python", op -> {1, 2}]ExternalOperation can be used as an argument to ExternalFunction:
ExternalFunction["Python", "len"][ExternalOperation["Eval", "[1, 2]"]]The executed operation is identical to the following one:
op = ExternalOperation["Call", ExternalFunction["Python", "len"], ExternalOperation["Eval", "[1, 2]"]]Which can be executed with ExternalEvaluate:
ExternalEvaluate["Python", op]The result is equivalent to running the following Python code:
ExternalEvaluate["Python", "len((1, 2, 3))"]session = StartExternalSession["Python"]Create an ExternalFunction and an ExternalObject:
func = ExternalFunction[session, "max"]obj = ExternalEvaluate[session, <|"Command" -> "[1, 2,3]", "ReturnType" -> "ExternalObject"|>]ExternalObject can be used as an argument to ExternalFunction:
func[obj]The result is equivalent to running the following Python code:
ExternalEvaluate["Python", "len((1, 2, 3))"]DeleteObject[session]session = StartExternalSession["Python"]Create an ExternalObject:
module = ExternalEvaluate[session, ExternalOperation["Import", "datetime"]]Use ExternalFunction to run methods on the object:
ExternalFunction[module, "date"][2012, 10, 10]DeleteObject[session]session = StartExternalSession["Python"]Create an ExternalObject for the datetime module:
datetime = ExternalEvaluate[session, ExternalOperation["Import", "datetime"]]Create an ExternalObject with the current date and another one with a timedelta:
{now, tenhours} = {datetime["datetime", "now" -> {}, ExternalObject], datetime["timedelta" -> {"hours" -> 10}, ExternalObject]}Send back the objects using an ExternalFunction:
ExternalFunction[session, "lambda obj, n: obj + n"][now, tenhours]The same can be done with an ExternalEvaluate call:
ExternalEvaluate[session, "lambda obj, n: obj + n" -> {now, tenhours}]Use "ReturnType" to return a String:
ExternalEvaluate[session, <|"Command" -> "lambda obj, n: obj + n", "Arguments" -> {now, tenhours}, "ReturnType" -> "String"|>]DeleteObject[session]Applications (3)
session = StartExternalSession["Python"]Define a function in the session:
ExternalEvaluate[session, "def collatz(n):
if n%2 == 0:
r = int(n/2)
else:
r = 3*n+1
return r
"]Create an ExternalFunction from this Python function:
collatz = ExternalFunction[session, "collatz"]Use the function within normal Wolfram Language code:
NestWhileList[collatz, 100, # =!= 1&]session = StartExternalSession["Python"]Import the Python NumPy package to load additional functionality:
ExternalEvaluate[session, "import numpy as np
def fun(list):
result = np.array(list).reshape(3,4)
return result
"]ExternalFunction[session, "fun"][Range[12]]It partitioned the list into a 3×4 matrix:
Normal[%]The equivalent Wolfram Language command:
Partition[Range[12], 4]session = StartExternalSession["Python"]Define a Python function that imports an image from a URL and rotates it:
ExternalEvaluate[session,
"import io
import requests
from PIL import Image
def rotateimage(url,angle):
response = requests.get(url)
img = Image.open(io.BytesIO(response.content))
img = img.rotate(angle)
b = io.BytesIO()
img.save(b, 'PNG')
return b.getvalue()"]The result is a ByteArray:
ba = ExternalFunction[session, "rotateimage"]["http://www.wolfram.com/language/img/wolfram-language-logo-home.png", 45]Import the ByteArray to display the rotated image:
ImportByteArray[ba, "PNG"]
DeleteObject[session]See Also
ExternalEvaluate ExternalObject ExternalOperation StartExternalSession APIFunction FunctionCompile CloudFunction Function
Formats: JavaScriptExpression PythonExpression
External Evaluation Systems: Python NodeJS Julia Java Octave Shell Ruby R Jupyter SQL SQL-JDBC CUDA
Function Repository: ToPythonFunction
Related Workflows
Text
Wolfram Research (2019), ExternalFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/ExternalFunction.html (updated 2024).
CMS
Wolfram Language. 2019. "ExternalFunction." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/ExternalFunction.html.
APA
Wolfram Language. (2019). ExternalFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExternalFunction.html
BibTeX
@misc{reference.wolfram_2026_externalfunction, author="Wolfram Research", title="{ExternalFunction}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/ExternalFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_externalfunction, organization={Wolfram Research}, title={ExternalFunction}, year={2024}, url={https://reference.wolfram.com/language/ref/ExternalFunction.html}, note=[Accessed: 13-June-2026]}