lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dirk Laurie <dirk.laurie@gmail.com>于2018年2月1日周四 下午4:22写道:
2018-02-01 0:08 GMT+02:00 Dibyendu Majumdar <mobile@majumdar.org.uk>:
> On 31 January 2018 at 22:03, Paul K <paul@zerobrane.com> wrote:
>>> require "paths"
>>> paths.require "libtorch"
>>
>>> The paths.lua script is generated from:
>>> https://github.com/torch/torch7/blob/master/paths.lua.in
>>> I must be missing something here.
>>
>> I think it's a different paths module. I'd expect it to be this one:
>> https://github.com/torch/paths
>>
>
> Ah okay thanks! Strange that there is 'paths.lua' in the Lua script folder.

The problem has two parts:

1. The name of the module is often a common word like "paths"
which may easily be non-unique.
2. The name under which the module is contributed to LuaRocks
is not the same as the name that comes in "require".

The Lua team has done what they could: "require" can specify
subpaths, which so far seem mostly to be exploited by writers
of large module collections.

What we as a community shoud do is to agree on a standard
directory tree to act as containers. just one or two levels.
Then we can say "paths = require 'torch.paths'" and make
sure we get the right one.

For this idea to work with "lua -l" , we also need the patch
to preload libraries with customized global names
   http://lua-users.org/wiki/LuaPowerPatches
so that

   lua -l paths=torch.paths

is possible.


I'd like the lua package system can support relative path like python.

So I use a simple function `import` to replace official `require` in my project:

local loaded = package.loaded
local searchpath = package.searchpath

function import(modname)
if modname then
local prefix = modname:match "(.*%.).*$" or (modname .. ".")
return function(name)
local fullname = prefix .. name
local m = loaded[fullname] or loaded[name]
if m then
return m
end
if searchpath(fullname, package.path) then
return require(fullname)
else
return require(name)
end
end
else
return require
end
end

And then put this line in the first line of every sub-module :

local require = import and import(...) or require