lunatic-python
lunatic-python copied to clipboard
cannot use python in lua
i use a raspberry pi 2 and 3 if i do py = require 'python'
this is what i get
py = require 'python' stdin:1: module 'python' not found: no field package.preload['python'] no file '/usr/local/share/lua/5.2/python.lua' no file '/usr/local/share/lua/5.2/python/init.lua' no file '/usr/local/lib/lua/5.2/python.lua' no file '/usr/local/lib/lua/5.2/python/init.lua' no file '/usr/share/lua/5.2/python.lua' no file '/usr/share/lua/5.2/python/init.lua' no file './python.lua' no file '/usr/local/lib/lua/5.2/python.so' no file '/usr/lib/arm-linux-gnueabihf/lua/5.2/python.so' no file '/usr/lib/lua/5.2/python.so' no file '/usr/local/lib/lua/5.2/loadall.so' no file './python.so' stack traceback: [C]: in function 'require' stdin:1: in main chunk [C]: in ?
any help
Most likely, your LUA_CPATH
has not been configured accordingly.
what should we do?
Two things come to mind, you can either move the python.so
into one of the default search paths that Lua looks in(eg. from the stacktrace) or you can add the location where python.so
exist to LUA_CPATH
(as a shell's env variable). Or you can also append that path to package.cpath
from Lua. For example
package.cpath = package.cpath .. ";/path_where_your/lunatic_python_binary_exist/goes_here/?.so"
py = require 'python'
-- ...
thanks i'll check
Hallo! I set LUA_CPATH as I see libs copied to site-packages export LUA_CPATH="/usr/lib/python2.7/site-packages/?.so;;" also change 'python' -> 'lua-python' $ lua test_py.lua lua: error loading module 'lua-python' from file '/usr/lib/python2.7/site-packages/lua-python.so': /usr/lib/python2.7/site-packages/lua-python.so: undefined symbol: _Py_ZeroStruct stack traceback: [C]: in ? [C]: in function 'require' test_py.lua:1: in main chunk [C]: in ? Press any key to continue...
Weee new issue:) What does it mean? Is "lua-python.so" the right lib to use in lua scripts? Why it is installed in python2.7/site-packages ? What should I do to fix?
Thanks in advance!
My .so files had much longer names. I ended up running into the same issue as you, @surfindominator .
@greatwolf , is there something we're getting wrong? I'd be happy to work with you to figure this out so we can get everyone an answer.
FYI, I'm using Python 3.5 on Ubuntu, with virtualenv. I'm curious what environments the other folks having this issue are using. Maybe there is a commonality?
What's the name of the lunatic .so built? What's the full path of its location? Also what version of lua/luajit is being used?
Note that lua C modules are sensitive to how the shared library is named. Specifically, the name of the *.so
needs to match entry function eg. python.so
needs to contain luaopen_python
. If the .so file gets renamed you'll also have to update the entry function name too or lua will fail to load it.
I'm using Lua 5.2.4. It builds two .so files:
copying build/lib.linux-x86_64-3.5/lua-python.cpython-35m-x86_64-linux-gnu.so -> /home/hosford4/.virtualenvs/testenv/lib/python3.5/site-packages
copying build/lib.linux-x86_64-3.5/lua.cpython-35m-x86_64-linux-gnu.so -> /home/hosford42/.virtualenvs/testenv/lib/python3.5/site-packages
I tried renaming each of them to python.so, and got the same error as @surfindominator for each file. I'm not sure how to go about renaming the entry function, but it looks like it's getting past that point considering the error message -- or maybe I'm misunderstanding.
@greatwolf, I looked a little closer and realized the message is not identical. It's failing to resolve PyFloat_Type
, not _Py_ZeroStruct
. After a bit of googling, I suspect maybe the issue is that libpython is not being linked against.
I think I found a bug in the setup.py script. In pkgconfig
, the for
loop that runs pkg-config breaks if the first call is successful. This means that the lua libs & cflags are captured, but the python libs & cflags are omitted. I modified the function to:
def pkgconfig(*packages):
# map pkg-config output to kwargs for distutils.core.Extension
flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
combined_pcoutput = ''
for package in packages:
(pcstatus, pcoutput) = commands.getstatusoutput(
"pkg-config --libs --cflags %s" % package)
if pcstatus == 0:
combined_pcoutput += ' ' + pcoutput
else:
sys.exit("pkg-config failed for %s; "
"most recent output was:\n%s" %
(", ".join(packages), pcoutput))
kwargs = {}
for token in combined_pcoutput.split():
if token[:2] in flag_map:
kwargs.setdefault(flag_map.get(token[:2]), []).append(token[2:])
else: # throw others to extra_link_args
kwargs.setdefault('extra_link_args', []).append(token)
if PY3:
items = kwargs.items()
else:
items = kwargs.iteritems()
for k, v in items: # remove duplicated
kwargs[k] = list(set(v))
return kwargs
After making this change, and renaming the .so files to lua.so and lua-python.so according to the filename prefixes, I was able to get past the require
statement without error.
I submitted a pull request for the bug fix. This should resolve the linking issues. The .so naming issues are still outstanding.
I'm running into a new issue now. After require
executes successfully, the python
global is nil. Any idea what might cause this?
I'm also getting warnings that python
is an undeclared global.
I submitted another pull request for the python
global being undefined.
@greatwolf, it seems that @hosford42 is actively helping in developing lunatic-python. Would you like to have him added as a collaborator?
@bastibe Sure thing, it would be nice to have multiple maintainers ;)
Well then, @hosford42, welcome to the team!
This comes with no obligations at all, but I am not the author of this package either. Think of it as a "community wiki" post on StackOverflow.
Thanks, guys! Not sure how much I can help, but happy to when I can.
On 10/07/2017 09:33 AM, Bastian Bechtold wrote:
Well then, @hosford42 https://github.com/hosford42, welcome to the team!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bastibe/lunatic-python/issues/41#issuecomment-334939179, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWls6JhisL8L_lcXpDQNrIq7ALN6p6Gks5sp4uzgaJpZM4J1uoH.
cp ./build/lib.linux-x86_64-2.7/lua-python.so ./test/python.so lua test_py.lua