r/learnpython • • 3h ago

how do i make an embedded lua interpreter?

im trying to make a project where in some parts you can run actual lua code. how would i make this and what libraries would i need (i already have lupa if that is usefull)

1 Upvotes

4 comments sorted by

2

u/lakseol 3h ago

Try searching on "lua in python". That finds things that you might be able to use, like this, amongst others:

https://luascripts.com/lua-in-python

1

u/ItsJustToastie 3h ago

i ment i would open a window with tkinter, then allow the user to code with lua in said window and run the code

2

u/lakseol 2h ago

For text-only output you could run the typed in code in a separate process that uses lua to execute the code, catch the text output and display that text in tkinter. Or have a look at:

https://luascripts.com/lua-in-python

which allows execution of lua code inside python. You would have to experiment with lupa to handle more than one lua statement. Maybe you could save the code into a temporary file and get lupa to execute that file.

1

u/RyanDubois43 2h ago

lupa.LuaRuntime() is what you want, then call.execute() on it to run lua code as a string. Remember that Lua variables and Python variables are not shared directly unless you pass them in.