extending lua load path

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

extending lua load path

oliver
Hello,
I have a lua interpreter embedded in my program and want to execute
some scripts.
The scripts are located in folder "scripts". One script requires
another like this:

1.script ("scripts/myscript.lua"):
require('base')
...
2.script ("scripts/base.lua"):
...

Now when I call my executable from within the "script" directory,
everything is fine since "." is in the Lua load path.
"executable myscript.lua"

But when I call it from anywhere else, the require "base" is not found:
"executable scripts/myscript.lua"
run-error - [string "script/myscript.lua"]:1: module 'base' not found..

In ruby I can resolve this issue by adding the directory of the
myscript.lua file to the load path:

$:.unshift File.dirname(__FILE__)
Can anybody tell me how I this is done in Lua?

Oliver

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

Duncan Cross
On Mon, Nov 14, 2011 at 4:47 PM, Oliver Mueller
<[hidden email]> wrote:

> Hello,
> I have a lua interpreter embedded in my program and want to execute
> some scripts.
> The scripts are located in folder "scripts". One script requires
> another like this:
>
> 1.script ("scripts/myscript.lua"):
> require('base')
> ...
> 2.script ("scripts/base.lua"):
> ...
>
> Now when I call my executable from within the "script" directory,
> everything is fine since "." is in the Lua load path.
> "executable myscript.lua"
>
> But when I call it from anywhere else, the require "base" is not found:
> "executable scripts/myscript.lua"
> run-error - [string "script/myscript.lua"]:1: module 'base' not found..
>
> In ruby I can resolve this issue by adding the directory of the
> myscript.lua file to the load path:
>
> $:.unshift File.dirname(__FILE__)
> Can anybody tell me how I this is done in Lua?
>
> Oliver
>
>

The load paths are taken from package.path - it is a
semicolon-separated string, where a question mark is used to
interpolate the package name. So, to look for modules inside the
'scripts/' folder, do this before any calls to require():

 package.path = package.path .. ';scripts/?.lua'

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

David Manura
In reply to this post by oliver
On Mon, Nov 14, 2011 at 11:47 AM, Oliver Mueller
<[hidden email]> wrote:
> One script requires another like this:
> 1.script ("scripts/myscript.lua"):
> require('base') ...
> 2.script ("scripts/base.lua"): ...
> But when I call it from anywhere else, the require "base" is not found:

My findbin and lib [1,2] modules (based on analogous modules from
Perl) had that in mind:

  -- /qux/bar/baz.lua
  print(require 'findbin'.bin) --> prints directory of script (/qux/bar)
  require 'lib' (require 'findbin' '/../lib')
     -- adds a path relative to the directory of the current script to the
     -- package search paths (package.path & package.cpath).
  require 'foo'  -- loads from new search paths (/qux/lib/foo.lua)

[1] https://gist.github.com/1342319
[2] https://gist.github.com/1342365

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

oliver
This post has NOT been accepted by the mailing list yet.
In reply to this post by Duncan Cross
This will work for exactly the scenario I described in my post...but unfortunately this is not general enough for me.
I don't know the path of "scripts" in advance (depends on where the user decides to store her scripts) so I cannot hard code it into the script.
I'd need to expand the load path with the path of the directory from which the first lua file is loaded.
Oliver
Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

Peng Zhicheng
In reply to this post by Duncan Cross
于 2011-11-15 1:56, Duncan Cross 写道:

> On Mon, Nov 14, 2011 at 4:47 PM, Oliver Mueller
> <[hidden email]>  wrote:
>
> The load paths are taken from package.path - it is a
> semicolon-separated string, where a question mark is used to
> interpolate the package name. So, to look for modules inside the
> 'scripts/' folder, do this before any calls to require():
>
>   package.path = package.path .. ';scripts/?.lua'
>
> -Duncan
>

just to mention, that the seperator/delimiter/placeholder of `package.path' is configurable,
not guaranteed to be semicolon or question mark.

refer to `package.config' and `luaconf.h' for details.




Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

oliver
Duncan's solution would work for exactly the scenario I described in
my post...but unfortunately this is not general enough for me. I don't
know the path of "scripts" in advance (depends on where the user
decides to store her scripts) so I cannot hard code it into the
script. I'd need to expand the load path with the path of the
directory from which the first lua file is loaded.
On Tue, Nov 15, 2011 at 4:33 PM, Peng Zhicheng
<[hidden email]> wrote:

> 于 2011-11-15 1:56, Duncan Cross 写道:
>>
>> On Mon, Nov 14, 2011 at 4:47 PM, Oliver Mueller
>> <[hidden email]>  wrote:
>>
>> The load paths are taken from package.path - it is a
>> semicolon-separated string, where a question mark is used to
>> interpolate the package name. So, to look for modules inside the
>> 'scripts/' folder, do this before any calls to require():
>>
>>  package.path = package.path .. ';scripts/?.lua'
>>
>> -Duncan
>>
>
> just to mention, that the seperator/delimiter/placeholder of `package.path'
> is configurable,
> not guaranteed to be semicolon or question mark.
>
> refer to `package.config' and `luaconf.h' for details.
>
>
>
>
>



--
tel +49 163 680 7658

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

oliver
In reply to this post by David Manura
nice, this comes very close to the ruby version...a little more
verbose but it will work for me.

On Tue, Nov 15, 2011 at 2:36 AM, David Manura <[hidden email]> wrote:

> On Mon, Nov 14, 2011 at 11:47 AM, Oliver Mueller
> <[hidden email]> wrote:
>> One script requires another like this:
>> 1.script ("scripts/myscript.lua"):
>> require('base') ...
>> 2.script ("scripts/base.lua"): ...
>> But when I call it from anywhere else, the require "base" is not found:
>
> My findbin and lib [1,2] modules (based on analogous modules from
> Perl) had that in mind:
>
>  -- /qux/bar/baz.lua
>  print(require 'findbin'.bin) --> prints directory of script (/qux/bar)
>  require 'lib' (require 'findbin' '/../lib')
>     -- adds a path relative to the directory of the current script to the
>     -- package search paths (package.path & package.cpath).
>  require 'foo'  -- loads from new search paths (/qux/lib/foo.lua)
>
> [1] https://gist.github.com/1342319
> [2] https://gist.github.com/1342365
>
>

--
tel +49 163 680 7658

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

Tom N Harris
In reply to this post by oliver
On 11/18/2011 03:35 AM, Oliver Mueller wrote:
> Duncan's solution would work for exactly the scenario I described in
> my post...but unfortunately this is not general enough for me. I don't
> know the path of "scripts" in advance (depends on where the user
> decides to store her scripts) so I cannot hard code it into the
> script. I'd need to expand the load path with the path of the
> directory from which the first lua file is loaded.

When you load the file, give it an environment with a version of
'require' that knows how to find the modules. Something like this,

local lua_require = require
local function build_require(path)
   path = path .. package.config:sub(1,1)
   return function(name)
            -- try to load path..name
            -- or pass the name to lua_require
          end
end
function doscript(filename)
   local env = setmetatable({}, {__index=_G})
   env.require = build_require(dirname(filename))
   return loadin(filename, env)
end

--
- tom
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: extending lua load path

Georg Lehner
In reply to this post by oliver
Hi!

One consequence of my proposal for a Lua virtual filesystem layer [1]
would be, that the module path could be a) reduced to "/lua/lib"
and b) extended at any moment by a union mount of the path "where
the user decides to store her scripts" before or after "/lua/lib".

As time permits, I will definitely pursue this path, extending my proof
of concept implementation[2].

Regards,

     Jorge-León

[1] http://lua-users.org/lists/lua-l/2011-11/msg00066.html
[2] http://sw.magma.com.ni/LuaFs


On 11/18/11 09:35, Oliver Mueller wrote:

> Duncan's solution would work for exactly the scenario I described in
> my post...but unfortunately this is not general enough for me. I don't
> know the path of "scripts" in advance (depends on where the user
> decides to store her scripts) so I cannot hard code it into the
> script. I'd need to expand the load path with the path of the
> directory from which the first lua file is loaded.
> On Tue, Nov 15, 2011 at 4:33 PM, Peng Zhicheng
> <[hidden email]>  wrote:
>
>> 于 2011-11-15 1:56, Duncan Cross 写道:
>>
>>> On Mon, Nov 14, 2011 at 4:47 PM, Oliver Mueller
>>> <[hidden email]>    wrote:
>>>
>>> The load paths are taken from package.path - it is a
>>> semicolon-separated string, where a question mark is used to
>>> interpolate the package name. So, to look for modules inside the
>>> 'scripts/' folder, do this before any calls to require():
>>>
>>>   package.path = package.path .. ';scripts/?.lua'
>>>
>>> -Duncan
>>>
>>>
>> just to mention, that the seperator/delimiter/placeholder of `package.path'
>> is configurable,
>> not guaranteed to be semicolon or question mark.
>>
>> refer to `package.config' and `luaconf.h' for details.
>>
>>
>>
>>
>>
>>
>
>
>