Lua require not working - lua

I am trying to make one lua file require another. I am following this guide: http://lua-users.org/wiki/ModulesTutorial
My basic test, which should be a trivial hello world, does not work, and I can't figure out why.
Here's a console log which shows all files and all errors:
C:\Users\TestUser\Desktop\LuaTest>dir
Volume in drive C has no label.
Volume Serial Number is XXXX-XXXX
Directory of C:\Users\TestUser\Desktop\LuaTest
11/15/2017 03:03 PM <DIR> .
11/15/2017 03:03 PM <DIR> ..
11/15/2017 02:53 PM <DIR> Bar
11/15/2017 03:04 PM 92 BazModule.lua
11/15/2017 02:53 PM <DIR> Foo
11/15/2017 03:08 PM 139 main.lua
2 File(s) 231 bytes
4 Dir(s) 253,774,073,856 bytes free
C:\Users\TestUser\Desktop\LuaTest>lua main.lua
lua: main.lua:1: module 'BazModule' not found:
no field package.preload['BazModule']
no file 'C:\dev\LuaDist\bin'
no file '.\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
stack traceback:
[C]: in function 'require'
main.lua:1: in main chunk
[C]: ?
C:\Users\TestUser\Desktop\LuaTest>type main.lua
local baz = require("BazModule")
baz.Baz()
local bar = require("Bar.BarModule")
bar.Bar()
local foo = require("Foo.FooModule")
foo.Foo()
C:\Users\TestUser\Desktop\LuaTest>type BazModule.lua
local BazModule = {}
function BazModule.Baz()
print("Hello Baz!")
end
return BazModule
C:\Users\TestUser\Desktop\LuaTest>lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
The expected output should be
Hello Baz!
Hello Bar!
Hello Foo!
But it can't find any of the files adjacent to main.lua and I don't understand why.

require searches in directories listed in package.path (for Lua files) and package.cpath (for compiled libraries).
Your error message…
lua: main.lua:1: module 'BazModule' not found:
no field package.preload['BazModule']
no file 'C:\dev\LuaDist\bin'
no file '.\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
indicates the paths that require searched in. It seems that package.path is completely empty, or maybe there's a single malformed path pattern in there. (Which would be C:\dev\LuaDist\bin.)
The way the search for a module foo.bar works is that ? is substituted by foo/bar (or foo\bar – depending on OS) and so ./?.lua would find ./foo/bar.lua.
So the way to fix this is to (a) fix the place where you (or something that you installed) are/is mangling the package.path (via environment variable, startup script, …?) and/or (b) add the current directory to the search path.

Related

Why -l option in lua interpreter acts strangely?

You can execute a sequence of chunks by giving them all as arguments
to the stand-alone interpreter, with the -l option. For instance, if
you have a file a with a single statement x=1 and another file b with
the statement print(x), the command line
prompt> lua -la -lb
will run the chunk in a, then the one in b, which will print the
expected 1.
The above is from the following link: https://www.lua.org/pil/1.1.html. Yet, when I was trying it out, I got a syntax error.
So, in file a.lua, I have only one line, which is a=1. Then in file b.lua, I have also only one line print("the value of a is:",a) . Then,
:~$ lua -i -la -lb
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
the value of a is: true
>
:~$
:~$ lua -la -lb
the value of a is: true
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
Why did it print out "the value of a is: true"? rather than "the value of a is: 1"?
Any comments are greatly appreciated.
The error is because you are use a = 1 in your file a.lua, unlike in the example where a.lua contains x = 1 and b.lua contains print(x).
Using a rather then x means your using the same a variable being written to when -la completes, changing it to true.
This happens because the option -l name is equal to name = require("name"). When require completes, on a file that returns no result to it, require will return true.
your command lua -la -lb would look something like this to lua:
a = require("a") --this returning true after it completes
b = require("b") --this printing the value of `a` which will always be `true`
Sources:
Similar question answered on Lua-Users: command-line -l option issue
Egor Skriptunoff's Comment on this question

Lua image errors

I have installed lua image according to this - link. I also tested my install with luajit -limage -e "image.test()" and it says 0 errors and 0 warnings. Also, when I try
> require 'image'
> l = image.lena()
There are no errors. However, when I try image.display(l), I get the following errors:
/home/srilatha/torch/install/share/lua/5.1/trepl/init.lua:384: module 'qt' not found:No LuaRocks module found for qt
no field package.preload['qt']
no file '/home/srilatha/.luarocks/share/lua/5.1/qt.lua'
no file '/home/srilatha/.luarocks/share/lua/5.1/qt/init.lua'
no file '/home/srilatha/torch/install/share/lua/5.1/qt.lua'
no file '/home/srilatha/torch/install/share/lua/5.1/qt/init.lua'
no file './qt.lua'
no file '/home/srilatha/torch/install/share/luajit-2.1.0-beta1/qt.lua'
no file '/usr/local/share/lua/5.1/qt.lua'
no file '/usr/local/share/lua/5.1/qt/init.lua'
no file '/home/srilatha/.luarocks/lib/lua/5.1/qt.so'
no file '/home/srilatha/torch/install/lib/lua/5.1/qt.so'
no file '/home/srilatha/torch/install/lib/qt.so'
no file './qt.so'
no file '/usr/local/lib/lua/5.1/qt.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'error'
/home/srilatha/torch/install/share/lua/5.1/trepl/init.lua:384: in function 'require'
/home/srilatha/torch/install/share/lua/5.1/image/init.lua:1363: in function 'display'
[string "_RESULT={image.display(l)}"]:1: in main chunk
[C]: in function 'xpcall'
/home/srilatha/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
...atha/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
[C]: at 0x00406670
I have torch running properly so I don't know what the problem would be with qt. How can I fix this?
Did you read this by any chance?
https://github.com/torch/image/blob/master/doc/gui.md
image.display among other functions can only be accessed via the qlua Lua interpreter.
Calling that function from LuaJit will result in the errors you're facing.
Just to add to Piglet's answer - read about qlua:
https://github.com/torch/qtlua/blob/master/doc/qt.md#qlua
At first I thought: "Oh dear, I'm using luajit, in a torch installation, I have no time and motivation to install another interpreter, configure luarocks to work with it, and then install everything from scratch". Good news: I was wrong.
qlua is an interpreter, but it gets installed in your existing Lua + Torch directory.
$ luarocks install qtlua
$ luarocks install qttorch
$ which qlua
/home/me/torch/install/bin/qlua
$ qlua
Lua 5.1 Copyright (...)
> require('image')
> l = image.lena()
> image.display(l)
>
.. a window pops up, and the prompt returns immediately.
This is because qlua has a background GUI thread to display your images and react to events (closing, and resizing the window, upon which the image will be scaled - with no aspect preservation: W & H independent).

lua load path for shared objects

I'm using lua 5.3beta under Kubuntu 12.04. I wrapped a c-extension using swig used and gcc4.9 for compiling and linking. If I place my mylib.so in the same directory my lua script is in:
require "mylib"
works fine. But if mylib.so is not within the same directory I get the error message
module 'mylib' not found
I added the path to mylib to LD_LIBRARY_PATH and inserted
package.path = package.path .. ';' .. path2mylib .. '/?
in my script. With
package.path = package.path .. ';' .. path2mylib .. '/?.so
the error message is:
mylib.so:1: unexpected symbol near '<\127>
I guess lua tries to load mylib.so assuming it is a lua-script.
It seems that lua looks for shared objects only within certain standard paths including ./ and all paths added to package.path are treated as paths to lua-files.
Is there any way to make lua load my c-extension without placing it into one of the standard library paths?
The path variable that tells Lua where to find libraries written in C is package.cpath.
package.path is for libraries written in Lua.

%PATH% on Windows 2008 Server actually not working

As you can see below, %AppData%\npm is added in the system Path variable. I've also tried adding it to the User Path variable. The file grunt.cmd exists in the directory, but only works if I specify it with an absolute path. AKA, path does not work.
C:\Users\TeamCity>echo %path%
%AppData%\npm;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows
\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\10
0\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program F
iles\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Ser
ver\100\Tools\Binn\VSShell\Common7\IDE\;c:\Program Files (x86)\Microsoft SQL Ser
ver\100\DTS\Binn\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program
Files\nodejs\
C:\Users\TeamCity>dir %appdata%\npm
Volume in drive C has no label.
Volume Serial Number is B845-1135
Directory of C:\Users\TeamCity\AppData\Roaming\npm
05/29/2013 03:14 AM <DIR> .
05/29/2013 03:14 AM <DIR> ..
05/29/2013 03:14 AM 298 grunt
05/29/2013 03:14 AM 159 grunt.cmd
05/29/2013 03:13 AM <DIR> node_modules
2 File(s) 457 bytes
3 Dir(s) 23,690,498,048 bytes free
C:\Users\TeamCity>grunt
'grunt' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\TeamCity>%appdata%\npm\grunt
grunt-cli: The grunt command line interface. (v0.1.9)
Running on:
Windows Server 2008 R2 Datacenter
Service Pack 1
64-bit
I don't think you can defer variable expansion in the path. What's in the path is used as is, it doesn't get re-interpreted during a search.
In fact, you must have worked really hard to get that in as %AppData% since you would need to do something like:
path=^%AppData^%\npm;%path%
If you were to just use:
path=%AppData%\npm;%path%
you'd find the actual path would be substituted, so that the path becomes:
C:\Users\TeamCity\AppData\Roaming\npm;C:\Windows\system32; ...
Interestingly enough, a last-ditch effort of "turning it off and on again" actually worked. Alternatively, you can copy the whole path after you added it and type in cmd:
> SET PATH=
and paste your path.

Lua change current/working dir, Linux (without LFS or any non-std modules)

I need to change current working dir in lua script for execute specific actions, but i have a trouble with this simple task.
I write test script test.lua :
os.execute("cd /usr")
os.execute("ls")
But lua test.lua output is:
test.lua
Current dir doesn't change. What's is wrong? And i can't use LFS or any non-std modules.
Thanks to all for explaining it situation. I choose another way : change work dir before run lua script, but i have a lot of troubles with paths which use in scripts and with scripts.
On Unix, os.execute() spawns a child process and its children. In
jpjacobs's answer, the first process would execute the shell.
The directory change operation only affects the child process, not the
process in which the Lua interpreter is executing your program.
It's simply not possible without external libraries. You can use stuff like
os.execute("cd /usr/ && ls")
The Lua standard library is intended to be both small and portable. Therefore, it is based on the capabilities of the C-standard library for all but a select few functions. It has no function to change directories; that's why libraries like LFS exist.
Have you considered that Lua may not be the appropriate language for your needs? If you're doing shell-style scripting work in an environment where you're not allowed to load non-standard modules, perhaps Python or Perl would be better for you. Both of them have extensive standard libraries with a host of features, all without the need to download non-standard modules.
If you want to do any real shell-style scripting in Lua, you need modules. It's that simple. So you should either use external modules or use a different language.
This is a bit of hack, but have you tried creating symbolic links to the folders you need in the current folder, so you don't have to change the current folder at all?
os.execute("ln -s /usr/foo") -- link called "foo" in current dir
os.execute("ln -s /usr/bar") -- link called "bar" in current dir
baz = require('foo.baz') -- loading file /usr/foo/baz.lua
Here is the program written using Zerobranie Lua 5.2:
local sP = "E:/Documents/Lua-Projs/SVN/ScriptsWireE2"
local a, b, c = os.execute("cd "..sP.."&& dir")
print(a,b,c)
And the outpuit:
Програма 'lua.exe' пусната в 'E:\Documents\Lua-Projs\ZeroBraineIDE\ZeroBraineProjects' (pid: 8832).
Volume in drive E is Data
Volume Serial Number is DE34-ED00
Directory of E:\Documents\Lua-Projs\SVN\ScriptsWireE2
08-03-2016 18:25 <DIR> .
08-03-2016 18:25 <DIR> ..
17-03-2016 18:15 <DIR> Bridges
07-03-2016 18:21 11 558 license
07-03-2016 18:21 87 readme.md
2 File(s) 11 645 bytes
3 Dir(s) 181 729 161 216 bytes free
true exit 0
Програмата завършена за 0.06 секунди (pid: 8832).

Resources