lua error loading local torch file - lua

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

Related

Lua: module 'castl.runtime' not found

I am new to Lua. I compiled a JS file to lua 5.2 using castl:
castl -o script.js
This created a file named script.js.lua. This is the first line of my new lua file:
local _ENV = require("castl.runtime");
I tried running that file with lua52 script.js.lua, but I got this error:
C:\Program Files\lua-5.2.4_Win64_bin\lua52.exe: script.js.lua:1: module 'castl.runtime' not found:
no field package.preload['castl.runtime']
no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime.lua'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime\init.lua'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.lua'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime\init.lua'
no file '.\castl\runtime.lua'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.dll'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
no file '.\castl\runtime.dll'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime52.dll'
no file '.\castl\runtime52.dll'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl.dll'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
no file '.\castl.dll'
no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl52.dll'
no file '.\castl52.dll'
stack traceback:
[C]: in function 'require'
script.js.lua:1: in main chunk
[C]: in ?
What am I missing? I am on Windows 10.
To answer your question
What am I missing?
Your problem is that you use a function require, but you don't know what this function does. Therefore, you cannot understand the errors it throws when it fails to do what you're expecting it to do.
So first refer to the Lua manual:
https://www.lua.org/manual/5.3/manual.html#pdf-require
There you'll find:
require searches for a Lua loader using the path stored in
package.path. If that also fails, it searches for a C loader using the
path stored in package.cpath. If that also fails, it tries an
all-in-one loader (see package.searchers).
and https://www.lua.org/manual/5.3/manual.html#pdf-package.path
The path used by require to search for a Lua loader.
At start-up, Lua initializes this variable with the value of the
environment variable LUA_PATH_5_3 or the environment variable LUA_PATH
or with a default path defined in luaconf.h, if those environment
variables are not defined. Any ;; in the value of the environment
variable is replaced by the default path.
For additional reading:
https://www.lua.org/pil/8.1.html
Entering "Lua require" into any web search will also result in plenty of solutions to your problem.
As hjpotter92 already stated in his comment, you have to tell your computer where to look for the files you want to require unless they are already located in the default folders.
Add the file's location to your LUA_PATH environment variable or add it to the package.path string before calling the respective require.
I'll leave an answer here since I believe that answer provided is actually not helping to someone who does not know anything about Lua ecosystem.
In order to require("castl.runtime") to be resolved one need to set LUA_PATH, in this case, for the sake of simplicity let's assume that castl distrib is located at /Users/me/castl, then you can:
export LUA_PATH="/Users/me/castl/lua/?.lua"
Now if you'll run Lua script it will be executed properly.

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).

luaj doesn't find .so modules

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/

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

Lua Relative Path Required

I am writing a Lua script and am trying to use require on a file that exists in the same directory as the main script. I cannot seem to get require to work in this case and have tried several solutions I have found but none seem to work. I have the following files together in a directory:
main.lua
helper.lua
I've tried the following solutions and gotten the error following each:
Solution 1:
local folderOfThisFile = (...):match("(.-)[^%.]+$")
local helper = require(folderOfThisFile .. 'helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: attempt to index local 'pathOfThisFile' (a nil value)
stack traceback:
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
Solution 2:
package.path = "/?.lua;" .. package.path
local helper = require('helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: module 'helper' not found:
no field package.preload['helper']
no file '/helper.lua'
no file '.\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.luac'
no file '.\helper.dll'
no file '.\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\loadall.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\loadall.dll'
stack traceback:
[C]: in function 'require'
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
I've tried variations on Solution 2 with various paths such as "?.lua;" and "./?.lua;" to no avail.
These two lines of the error message shed some light on your problem:
no file '/helper.lua'
no file '.\helper.lua'
The first line is due to your change to package.path. As you can see, it looks for a "/helper.lua" file that doesn't exist so its not doing anything. The second line is due to the default package.path and is looking for a "helper.lua" in the current working directory. Since its not finding, your current working directory must not be the directory your main.lua is located on.
The fix is to either make the current working directory the directory where main.lua and helper.lua are located or to add "C:\\path\\to\\your\\lua\\project\\?.lua" to the package.path
i am still learning lua but here is what I can whip up for you,
if you don't have a file system API installed then you can make a string variable with your curent working dir in it and you can add to it like this
local cwd="C:\users\user\Desktop\"
dofile(cwd.."program.lua")
thats what I do, and I have no problems with it
If you mean that you want to be able to invoke the program from any directory and that it correctly locates the required files, then you can use this solution (you only need it within main.lua):
local base_path = string.match(arg[0], '^(.-)[^/\\]*$')
package.path = string.format("%s;%s?.lua", package.path, base_path)
This works by adding the directory where the file is to the package path, so that require can work on the files in that directory. Lua doesn't do this automatically yet (Python does, since version 2.6 or so); hopefully it will be implemented in future. You can also use base_path to refer to other files in the same directory. In my case, for example, there is a SQLite database in that directory and the program needs to open it, so I use this:
local database_filename = base_path .. 'db.sqlite'
You can also make base_path a global so that it's available to other modules if necessary.

Resources