Lua: require fails to find submodule, but searchpath succeeds? - lua

usually when I have a question about something remotely software related I find that someone else has already asked the very same thing, and gotten good answers that works for me too.
This time, though, I've failed to find an answer to my predicament.
Here we go:
I'm currently trying to move up my Lua-programming a notch or three and want to use modules. So, I've got a structure like this:
main.lua
foo/bar.lua
Now, in main.lua I do
require("foo.bar")
which fails,
main.lua:1 module 'foo.bar' not found:
no field package.preload['foo.bar']
no file 'foo.bar.lua'
no file 'foo.bar.lua'
no file 'foo.lua'
Ok, something might be wrong with my package.path so I use package.searchpath("foo.bar", package.path) to see what I', doing wrong.
The problem is that package.searchpath resolves foo.bar to foo/bar.lua which is exactly right.
As I've understood it, package.searchpath tries to find the module in the same way as require, but there seems to be som glitch in my case.
What strikes me as odd is the repetition of the no file 'foo.bar.lua' in the error output
Have I misunderstood the use of require?
I'm using LuaJIT-2.0.0 to run my chunks
Update:
I'm using LuaJIT-2.0.0 to run my chunks <- This was the reason for my problem, stock Lua-5.2.2 behaves as expected

package.path = debug.getinfo(1,"S").source:match[[^#?(.*[\/])[^\/]-$]] .."?.lua;".. package.path
require("foo.bar")
This line causes require to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory, insert the
relative path between " and ?.lua
Here is part of require description:
[...] Otherwise 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.loaders).
Default path for package.path is always the .exe that executes specified script.

Related

Lua require error if script is called "table.lua"?

Trying to replicate this simple Lua example (using the improved code in the second post), I encountered the following strange issue:
I copied the code verbatim, but happened to call the first file "table.lua" (instead of "funcs.lua").
The second file was called "main.lua" as in the example.
In my case, whatever I tried, I invariably got the popular error message "attempt to call field 'myfunc' (a nil value)" (as if the require statement had been ignored; but path etc. were all in order).
After two hours of trying and hunting for info, I more or less on a hunch renamed the first file from "table.lua" to "tabble.lua", and then everything promptly worked as expected. Renaming to e.g. "tables.lua" will also work.
Being very new to Lua, I'd still like to understand what exactly went wrong. Initially I thought the reason might be that "table" is a reserved Lua word, but all references I checked do not list it as such.
So what is going on here?
I am using LuaForWindows v5.1.4-46 with the included SciTE editor/IDE (v.1.75).
Thanks for all hints.
The standard libraries math, io, string, …, and table are pre-defined (and pre-loaded) in the Lua interpreter. Because require caches modules by name, saying require "table" will return the standard table library instead of loading your own table module from a file.
A good way to solve the problem is to create a folder and put your library files in there. If the folder is called mylib, then require "mylib.table" will work and load the file.
Alternatively, if you just need to load the file once and do not need the features of require (searching the file in a number of directories, caching loaded libraries), you can use loadfile: Change require "table" to loadfile "./table.lua" () (where ./table.lua should be the full (relative is fine) path to the file.)

in lua (love2D), i want to import a library in a subfolder, the file in there will not find the next module it requires

So in lua, i want to import a module.
I want to have my "polygon" lib in a subfolder, so i reference it like this
local polygon = require('polygon.polygon')
however, it needs another module called 'delaunay', it cannot find it as it checks the main folder
Is there anyway short of editing my library, to get this to work? (some kind of ability to add search paths?)
Thanks
To know where to look for modules, Lua's require uses the variables package.path (.lua) and package.cpath (.so/.dll). You can change them in your program to look in the directory you have it in. For consistency's sake, you can look at their contents to know which OS-specific separator to use. For example:
local sep = package.path:find("\\") and "\\" or "/"
package.path = package.path .. ";." .. sep .. "polygon" .. sep .. "?.lua"
This would include ./polygon/?.lua into the search path, and a call to require "delaunay" would therefore have the require function look for ./polygon/delaunay.lua in addition to existing paths. Bear in mind that in require strings, . denotes a separator as far as file searching is concerned, so calling require "polygon.delaunay" in this scenario will mean searching for ./polygon/polygon/delaunay.lua.
From what I understand of your question, changing the package.path variable to include the path to where your delaunay library is stored would solve your issue, although to give a specific solution more information about your project and directory structure is required.
All what loaded into package.loaded can be load with require() because require() looks first in package.loaded...
-- Lua 5.3 ( lua -i )
> package.loaded.code=load(code.dump)()
> test=require('code')
> test
function: 0x565cb820
So you can use load() or loadfile('/path/to/your_code.lua') to do this. Another nice feature of this method is to load dumped code...
> package.loaded.shell=loadfile('shell.bin')
> shell=require('shell')
> shell('cat shell.bin')
uaS�
�
xV(w#F#�d�#��F�#G���d#��F�#G���d#&�typestringoexecute
/bin/bash>
In any way, it appears that you will have to deal with the subfolders explicitly.
As in, either the module polygon will have to import delaunay as polygon.delaunay.
Or module names will have to be appended to package.path so that lua could search through subfolders for filenames:
package.path=package.path..";./polygon/?.lua"
More info is here.
It is pointed out in comments, you'd probably want to ensure that path concatenation happens only once.
Also, one should be wary of name shadowing.
Finally, while we are at stirring up the past, a pretty nifty trick to solve the issue was proposed here five years before the question.

Lua Sockets Module cpath

I'm trying to use the sockets module in a script and I keep encountering a issue where the script is unable to find socket.core. Is there anyway for me to point to exactly where the core.dll is? I've tried using cpath and I can never seem to get it to work. I just want to be able to say "C:/folder/folder/folder/core.dll"
package.cpath = 'F:/Folder/Foldertwo/Game/agame/Beta/Scripts/libs/socket/?.dll;' .. package.cpath
#EgorSkriptunoff is correct in his comment: socket.lua (which is a lua module) loads socket.core (which is a dynamic library), so you won't be able to load it from folder/core.dll as the default searcher will be looking for socket/core.dll.
If you really want to load it from folder/core.dll, you may try to load it yourself and assign the returned value to package.preload['socket.core']. This way when socket.lua loads the module, it will get the value to return from package.preload key without loading the module.

Is there any way that prevent lua from translating 'a.b' to 'a/b' when I require 'a.b'?

For example:
require('a.b.c/foo.lua')
Lua engine will translate 'a.b.c/...' to 'a/b/c/...' to search file in the pattern list, right?
Will there be any problem when 'a.b.c' are real folder name? If so, how to solve that?
If your module is located in a directory that contains . as part of its name, pulling it in with require will be difficult since there's no way to 'escape' that . so the \ substitution isn't done.
However, it may still be possible to point lua to look in the right place indirectly by playing with the package.path:
local restorepath = package.path
package.path = "./a.b.c/?.lua"
require 'foo'
package.path = restorepath
But I'd recommend that you try to reorg your project directory structure first so it better works with lua's require. Otherwise you'll have to do keep doing the above dance with other modules you might have.
Such translation is not on the spec, as far as I know. So it might work, but it might also not work. It might break on some platforms (i.e. work on Linux and Mac and not on Windows). It might work in one version of Lua and not on the next. So I would not recommend it.
The way to make sure you stay platform-agnostic is using dots everywhere. Also, I would recommend not putting the .lua part at the end, require does not always resolve that well either:
require('a.b.c.foo')

Python kivy filechooser reads wrong path

I'm trying to use Kivy as a GUI for my python app, which needs to read a file from the filesystem.
However, in some cases the kivy filechooser read wrong path or nothing causing an IndexError while I'm trying to set the read path for a text of a textfield.
I use the default example for reading files learned from http://kivy.org/docs/api-kivy.uix.filechooser.html
The relevant part of my app is in this function, where an exception handling is added as a not a good approach to handle this :)
def load(self, path, filename):
'''
this will load the file and dismiss the dialog
'''
print "Loading file..."
print "filename:",filename
print "path:",path
try:
self.selected_file = filename[0]
self.file_text_input.text = self.selected_file
self.dismiss_popup()
except IndexError as ie:
print "Something made a boo-boo...try again"+str(ie)
self.dismiss_popup()
self.show_popup("ERROR","Somehow I couldn't load the file:\nCheck the permissions or move it to other place")
self.show_popup() is just a helper function, which shows a popup with the set function params.
The basic error is that filename[0] will throw an IndexError since it does not read the correct path.
I'm using Linux with python2.7, and somethimes when I select a file in my home folder, the filename variable stores nothing, while path variable stores misteriously a random folder, for instance, /media, /opt, etc.
Did anyone meet this issue?
I found out why it was handled uncorrectly.
All the failures are caused by Kivy's
FileChooserListView
, which enables to click on folders and files via a list, but it also makes it possible to have a littel '>' sign at the beginning of every list element, which are directories.
I realized that when I used these '>' signs, then I get wrong path, but if I always clicks on the directories' list elements then everything works fine.
However, that little '>' cannot be disabled (for now), so the best and fastest alternate solution is using
FileChooserIconView
instead!
Now everything is good :)

Resources