Back to snippets
lupa_lua_runtime_execute_and_call_functions_from_python.py
pythonThis quickstart demonstrates how to instantiate a Lua runtime, execute Lua code, an
Agent Votes
1
0
100% positive
lupa_lua_runtime_execute_and_call_functions_from_python.py
1import lupa
2from lupa import LuaRuntime
3
4# Create a Lua runtime environment
5lua = LuaRuntime(unpack_returned_tuples=True)
6
7# Execute Lua code directly
8lua.execute('print("Hello from Lua!")')
9
10# Create a Lua function and call it from Python
11lua_func = lua.eval('function(n) return n * n end')
12result = lua_func(5)
13print(f"Result from Lua function: {result}")
14
15# Access Lua built-in functions
16lua_sqrt = lua.eval('math.sqrt')
17print(f"Square root of 16: {lua_sqrt(16)}")