luaj doesn't find .so modules - lua

I've written a GUI in Java and use a lua-script to calculate some values for images with a neural network. Therefore the lua-script requires some modules from torch7.
I got so far that it finds the modules which have a init.lua file. However, it fails when a module only has a .so file. The module is required in one of the init.lua files.
Before I require the modules in the script i set the new package.path and the package.cpath to LUA_PATH and LUA_CPATH because luaj only used the default path which didn't work.
I think this a LuaJ problem because when I run the script in the terminal with lua script_name.lua it works fine.
package.path = package.path .. ';/home/user/.luarocks/share/lua/5.1
/?.lua;/home/user/.luarocks/share/lua/5.1/?/init.lua;/home/user/torch
/install/share/lua/5.1/?.lua;/home/user/torch/install/share/lua/5.1
/?/init.lua;./?.lua;/home/user/torch/install/share/luajit-2.1.0-
beta1/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1
/?/init.lua'
package.cpath = '/home/user/torch/install/lib/?.so;/home/user/.luarock
/lib/lua/5.1/?.so;/home/user/torch/install/lib/lua/5.1/?.so;./?.so;
/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so'
require 'torch'
require 'nn'
require 'image'
The error message is as follows:
exception in thread "main" org.luaj.vm2.LuaError: #/home/user/torch/install/share/lua/5.1/paths/init.lua:1 module 'libpaths' not found: libpaths
no field package.preload['libpaths']
libpaths.lua
/home/user/.luarocks/share/lua/5.1/libpaths.lua
/home/user/.luarocks/share/lua/5.1/libpaths/init.lua
/home/user/torch/install/share/lua/5.1/libpaths.lua
/home/user/torch/install/share/lua/5.1/libpaths/init.lua
./libpaths.lua
/home/user/torch/install/share/luajit-2.1.0-beta1/libpaths.lua
/usr/local/share/lua/5.1/libpaths.lua
/usr/local/share/lua/5.1/libpaths/init.lua
no class 'libpaths'
I call the script from my java program:
public ScoreImage(){
G_ = JsePlatform.standardGlobals();
//G_.get("dofile").call( LuaValue.valueOf(changePath_));
G_.get("dofile").call( LuaValue.valueOf(script_));
}
I use lua 5.1 because some of the problems where solved by changing from lua 5.2 to lua 5.1. I have Ubuntu 14.04 LTS and luaj 3.0.1.
I really appreciate any help!

It seems that LuaJ does not have the ability to load dll files and so files.
You may want to look into jnlua:
https://code.google.com/archive/p/jnlua/

Related

lua error loading local torch file

I have a file named opts.lua. When i'm trying to load the local module using require opts in main.lua, it is always searching in ~/torch/install/share/lua/5.1 and getting this error:
No luarocks module found for opts.
Both main.lua and opts.lua are in same directory. Please help me in solving this.
Note: I am running the main file in ~/torch/install/bin using ./th main.lua

Error loading module when using lua

I'm new to lua and recently learning DL with Torch.
I have installed torch just following instructions: http://torch.ch/docs/getting-started.html#_ and added some packages using luarocks install. Then I wrote a test file:
require 'torch'
require 'nn'
--[[do something]]
when running with lua test.lua (Ubuntu 14.04), it errs as followed:
error loading module 'libpaths' from file
'/home/user1/torch/install/lib/lua/5.1/libpaths.so':
/home/user1/torch/install/lib/lua/5.1/libpaths.so: undefined symbol:
luaL_register
It seems something wrong with path settings or so. However, when I run test with command th, it works fine.
I searched and examined these answers: Error loading module (Lua)
Torch7 Lua, error loading module 'libpaths' (Linux)
not fully answered my question though.
So I wonder where exactly the error comes from, and how to fix it. Even though I can use torch with th.
ADD:
I find that the reason maybe API luaL_register is not supported in ver 5.2 which is what I am using, while th calls a lua shell in ver 5.1? So does this mean I can only use th to run my files?
You are likely using your system Lua (probably version 5.2), but Torch requires LuaJIT it comes with. Run your script as luajit test.lua (it's probably in /home/user1/torch/install/bin/luajit).

Require doesn't find shared libraries

I'm trying to run a neural network from torch inside Java. I'm using luaj as a wrapper.
The problem is that when I require for example the torch module which depends on the libpaths module which is a shared library it throws the following error:
module 'libpaths' not found: libpaths
no field package.preload['libpaths']
libpaths.lua
Before I require any module I set the package.cpath and package.path to the folders where the libraries are because before it only looked in the default path which was just the project folder. However I have the feeling that 'require' only looks in the package.path for modules and not in the cpath because then it would also find libpaths.so. Also because this output includes only the directories from path and not cpath:
/home/erika/.luarocks/share/lua/5.1/libpaths.lua
/home/erika/.luarocks/share/lua/5.1/libpaths/init.lua
/home/erika/torch/install/share/lua/5.1/libpaths.lua
/home/erika/torch/install/share/lua/5.1/libpaths/init.lua
./libpaths.lua
/home/erika/torch/install/share/luajit-2.1.0-beta1/libpaths.lua
/usr/local/share/lua/5.1/libpaths.lua
/usr/local/share/lua/5.1/libpaths/init.lua
I tried package.searchpath() with the path from the cpath and 'libpath' and it did find the libpaths.so. I tried a lot of different things but nothing seems to work so I would really appreciate some help!
I use Lua 5.1.5, my OS is Ubuntu 14.04 LTS.
When you're using luarocks to install packages locally, you should setup your environment before starting lua/luajit/torch. The easiest way is to execute the output of luarocks-5.1 path
$ luarocks-5.1 path
export LUA_PATH='/home/markus/.luarocks/share/lua/5.1/?.lua;/home/markus/.luarocks/share/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;./?.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua'
export LUA_CPATH='/home/markus/.luarocks/lib/lua/5.1/?.so;/usr/lib/lua/5.1/?.so;./?.so;/usr/lib/lua/5.1/loadall.so'
A more comfortable way is to put the output into your ~/.bashrc
$ luarocks-5.1 path >> ~/.bashrc
To reload your ~/.bashrc without logout and login, do source ~/.bashrc

How to set the environment variable of zerobrane studio

I install all torch package into my local file torch-distro(Followed by this tutorial). I want to use Zerobrane to debug my code. Zerobrane can't find my local path of torch. How Can I set my local path to the Zerobrane environment variable.
I tried to add path.lua = "${prefix}/torch-distro/install/bin/luajit" into the user.lua. But it can't work
Following method works on linux platform:
Configuring the luajit interpreter by adding following code into the user.lua
path.lua = "your_path/luajit"
Configuring the envrioment variable by adding following code into the /opt/zbsstudio/lualibs/mobdebug/mobdebug.lua
package.path = package.path .. ';my_path/?/init.lua'
package.cpath = package.cpath .. ';my_path/?.so'
(These instructions are for the Windows version of Torch, but the steps should work for Linux/OSX versions assuming the paths are modified).
Let's say the Torch is installed in C:\Program Files\Torch, then to get it running as the external interpreter from ZeroBrane Studio (ZBS), you need to add path.lua=[[C:\Program Files\Torch\bin\torch-lua]] to <ZBS>\cfg\user.lua configuration file.
Now, when you execute a Lua script from ZBS (Project | Run or F6), it will run inside the Torch environment:
local torch = require 'torch'
local data = torch.Tensor{
{68, 24, 20},
{74, 26, 21},
{80, 32, 24}
}
print(data)
However, there are few more steps required to get the debugging to work on Windows (these steps are likely not be needed on other systems, but I haven't tested debugging there). ZBS is using luasocket, which is compiled against lua51.dll, but Torch is using libtorch-lua.dll, so loading luasocket into your (Torch) process is likely to crash it. To make it work, you need to build a proxy DLL and put it into your Torch/bin folder.
To build the proxy DLL, you will need Visual Studio C++ or mingw/gcc compiled and can follow these steps:
Get mkforwardlib.lua (VS) or mkforwardlib-gcc.lua (mingw/gcc) script from Lua Proxy DLL3 page.
Run lua mkforwardlib.lua libtorch-lua lua51 X86; if everything goes well, this will produce lua51.dll file in the current folder.
Copy lua51.dll file to Torch\bin folder.
Now you should be able to debug Torch scripts by using Project | Start Debugging.

Notepad++ sets incorrect path when running script

I have a simple script that I want to import into another with require, but when I run it from Notepad++ I get the usual error that require produces.
The funny thing is that it worked an hour ago and I did not restart the computer since then.
The files are in the same directory, so the simple file name (without .lua) worked and should still work. (relative path)
Lua runs the script just fine.
this is what I entered in Notepad:
cmd /k lua "$(FULL_CURRENT_PATH)"
Earlier I also had a problem with Penlight, maybe there is some connection, so here it is:
I tried to require"pl" but it failed to find the module. (ran from SciTE, worked prevously)
I tried it in the Lua command line and it worked like a charm.
Tried again in SciTE and voila it worked again.
I have no idea what causes any of them.
ps.: using the lfs module and os.execute("cd /d ...path...") did not work
Lua is searching for your required module in the folders of LUA_PATH. In the script you run via F5, put this statement:
print('current path is:')
os.execute('cd')
require 'someModuleThatDoesntExist'
After printing the "working" forlder (Program Files/Notepad++), it tries to find the required module and fails. The traceback shows that Lua looks through many different folders, none of them being the one containing FULL_CURRENT_PATH, so it can't find the module.
You have several choices:
put your scripts in one of the listed paths
set LUA_PATH in your environment to contain the folder name where your scripts are located
change package.path from your script so it knows where to look for other modules. You could do this by either:
including an extra parameter to your F5, namely CURRENT_DIRECTORY, and make your script take its first command line param (CURRENT_DIRECTORY) to add it to package.path
parse arg[0] when your script starts, to get the folder containing script, and extend package.path
For example with #3, first option, you would use
cmd /k lua "$(FULL_CURRENT_PATH)" "$(CURRENT_DIRECTORY)"
in notepad++ and in your Lua module you would use
thisModuleDir = arg[1]
package.path = thisModuleDir .. ";" .. package.path
require 'yourmodule'

Resources