2

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

Richard
  • 135
  • 2
  • 11
  • 1
    provide the error message and more info on your folder structure – Piglet Aug 08 '17 at 18:21
  • Lua has problems with supporting relative paths for modules. – Egor Skriptunoff Aug 08 '17 at 18:46
  • If delaunay is in the polygon subfolder, you could use require('polygon.delauny'). A better solution might be to make sure both files are findable through package.path (as explained in the other answers) and just use require('polygon') and require('delaunay'). That way, if you were to change your directory structure, you only need to change package.path. – Xeozim Aug 09 '17 at 12:45

3 Answers3

0

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.

Dimitry
  • 2,204
  • 1
  • 16
  • 24
  • this is bad idea. package.path is global. Each request, this code executed and concate infinitely. At the end, it will explose. I have a bad experience in PROD with this code. Please use lua_package_path instead. Read this article also https://medium.com/@fabricebaumann/how-we-reduced-the-cpu-usage-of-our-lua-code-cc30d001a328 – uwevil Nov 20 '20 at 09:05
  • 1
    @uwevil I'm not sure how web servicing works, but in LÖVE2D you would put this code inside `love.load()` function that is run only once at the initialization of the program. It seems from google, that `lua_package_path` is nginx-specific feature. – Dimitry Jan 26 '21 at 14:45
0

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.

EngiN33R
  • 34
  • 3
0

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>
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15