| name | bx-jython |
| description | Use this skill when executing Python code inside BoxLang with the bx-jython module: jythonEval(), jythonEvalFile(), the bx:jython component, variable bindings from the BoxLang variables scope, and inter-language result handling. |
bx-jython: Execute Python in BoxLang
Installation
install-bx-module bx-jython
box install bx-jython
BIFs
| BIF | Description |
|---|
jythonEval( code ) | Execute a Python code string |
jythonEvalFile( path ) | Execute a Python file |
Basic Usage
jythonEval( "print('Hello from Python!')" )
jythonEvalFile( "/app/scripts/process.py" )
jythonEval( "
result = 5 * 10
print('Result:', result)
" )
Variable Bindings
BoxLang automatically binds all variables scope values into the Python engine. Use them as native Python variables:
variables.name = "Luis Majano"
variables.message = "Hello from BoxLang"
jythonEval( "print(name)" )
jythonEval( "print(message.upper())" )
Using Python Modules (Files)
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
jythonEvalFile( "/app/scripts/mathutils.py" )
result = jythonEval( "factorial(10)" )
fib = jythonEval( "fibonacci(20)" )
println( result )
println( fib )
bx:jython Component
Use the component for larger multi-line Python blocks captured in a variable:
bx:jython variable="result" {
writeOutput( "
x = 10
y = 20
sum = x + y
product = x * y
print(f'Sum: {sum}, Product: {product}')
" )
}
Capturing the Result
The jythonEval() / jythonEvalFile() functions return a struct:
engineResult = jythonEval( "42 + 8" )
engineRef = engineResult.engine
globalScope = engineResult.globalScope
engineScope = engineResult.engineScope
Common Patterns
variables.numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
jythonEvalFile( "/app/scripts/statistics.py" )
mean = jythonEval( "mean(numbers)" )
stddev = jythonEval( "std_dev(numbers)" )
Common Pitfalls
- ✅ All variables in the BoxLang
variables scope are automatically accessible in Python — no explicit passing needed
- ❌ Python uses indentation for blocks — ensure your code strings use consistent indentation
- ✅ Use
jythonEvalFile() for complex Python logic; use jythonEval() for quick invocations
- ❌ Jython runs Python 2.7 syntax (Jython's Python compatibility level) — not Python 3.x