14

I want to run a python script from my lua file. How can I achieve this?

Example:

Python code

#sum.py file
def sum_from_python(a,b)
    return a+b 

Lua code

--main.lua file
print(sum_from_python(2,3)) 
cubuspl42
  • 7,833
  • 4
  • 41
  • 65
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
  • 1
    I added my answer, but some information like Lua implementation you use, Python version you use, Lua version you use, size of your project that you need to wrap, etc. would be very helpful. Are you tied to specific implementations/versions or maybe you are flexible? Do you want to call simple global functions, or maybe you need deep inter-language integration? You are not new to Stack Overflow. – cubuspl42 Dec 05 '13 at 12:51
  • i need to call base64 , pickle library into lua – Prashant Gaur Dec 05 '13 at 13:48
  • Prashant Gaur, I'd say base64 is trivial enough to implement it with plain Lua. Not sure about `Pickle` serialization, but quick googling came up with this Lua [example int Lua-users community wiki](http://lua-users.org/wiki/PickleTable). – Kamiccolo Dec 05 '13 at 14:04
  • 3
    You just need the functionality of that two libraries for use with Lua variables? You want to serialize Lua tables with pickle and use base64 with Lua data? Then calling Python from Lua seems a bit crazy idea. Try to use some Lua packages or find some plain-C libraries and load them into LuaJIT if you can use that fast Lua implementation. – cubuspl42 Dec 05 '13 at 14:58
  • See also [How to embed Lua inside Python? - Stack Overflow](https://stackoverflow.com/questions/6234252/how-to-embed-lua-inside-python) – user202729 Oct 26 '21 at 11:27

3 Answers3

7

Sounds like Lunatic-Python does exactly what you're looking for. There's a fork of lunatic-python that's better maintained than the original. I've contributed several bug fixes to it myself awhile back.

So reusing your example,

Python code:

# sum.py
def sum_from_python(a, b):
  return a + b

Lua code:

-- main.lua
py = require 'python'

sum_from_python = py.import "sum".sum_from_python
print( sum_from_python(2,3) )

Outputs:

lua main.lua
5

Most of the stuff works as you would expect but there are a few limitations to lunatic-python.

  1. It's not really thread-safe. Using the python threading library inside lua will have unexpected behavior.
  2. No way to call python functions with keyword arguments from lua. One idea is to emulate this in lua by passing a table but I never got around to implementing that.
  3. Unlike lupa, lunatic-python only has one global lua state and one python VM context. So you can't create multiple VM runtimes using lunatic-python.

As for lupa, note that it is a python module only, which means you must use python as the host language -- it does not support the use-case where lua is the "driving" language. For example, you won't be able to use lupa from a lua interpreter or from a C/C++ application that embeds lua. OTOH, Lunatic-Python can be driven from either side of the bridge.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
4

I see these as your options:

  • Don't do it: I agree with others' suggestions that you should find a way to do it in pure Lua, but perhaps you have a real requirement to integrate the two.

  • You could use SWIG (www.swig.org) to export the Lua C API to Python. You might save yourself some time by using a C++ binding (like lua-icxx.sf.net) but that really depends on your requirements.

  • You could use an existing library; lunatic python is dead AFAIK, but LUPA seems in good health (https://pypi.python.org/pypi/lupa).

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • As far as I can see lupa only allow you to start Lua inside Python. If there's no existing Python then it can't help. (see also the remark at the end of the other answer) – user202729 Oct 26 '21 at 12:33
1

You can try this library or write some bridge specific for your project, but it would require good knowledge of both Lua C-API and Python C-API.

David Doria
  • 9,873
  • 17
  • 85
  • 147
cubuspl42
  • 7,833
  • 4
  • 41
  • 65