Link Python to LuaJIT

Being able to call Python from LUA allows a developer to utilitise its familiarity, expressiveness and rich library.

LuaJIT can easily link with Python APIs. Thisexample creates a Python function, calls it and extracts the return value. This can be used as a basis to build a complete solution using Python in conjunction with the Visionary Render Platform.


The following example requires the correct path to the Python DLL Version 3 of the Python is used, earlier versions are not compatible and minor changes to the API function names are required.

local ffi = require("ffi")
-- be sure to use the correct path below.
local py = ffi.load("C:\Users\Your.Username\AppData\Local\Programs\Python\Python37\Python37.DLL")
void Py_Initialize();
void Py_Finalize();
int PyRun_SimpleStringFlags(const char *cmd, void *flags);
void *PyImport_Import(void *str);
void *PyObject_GetAttrString(void *module, const char *fun);
void *PyObject_CallObject(void *func, void *args);
void *PyTuple_Pack(int n, ...);
void *PyFloat_FromDouble(double d);
double PyFloat_AsDouble(void *d);
void *PyUnicode_FromFormat(const char *str, ...);
local i = py.PyRun_SimpleStringFlags([[
def mul(a, b):
  return a * b
]], nil)
local moduleMainString = py.PyUnicode_FromFormat("__main__")
local moduleMain = py.PyImport_Import(moduleMainString)
local func = py.PyObject_GetAttrString(moduleMain, "mul")
local arga = py.PyFloat_FromDouble(12.0)
local argb = py.PyFloat_FromDouble(10.0)
local args = py.PyTuple_Pack(2, arga, argb);
local ret = py.PyObject_CallObject(func, args);

Ericka is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.