lua-users home
lua-l archive

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

On Mon, Nov 14, 2011 at 4:47 PM, Oliver Mueller
<> 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'