How to change package.path?

Posted by Narci on Sun 27 Jul 2014 10:52 AM — 2 posts, 12,706 views.

#0
How to change package.path?
Australia Forum Administrator #1
You can't change that, it seems, but you can load from other places easily enough.


For a start you can load extra code yourself with "dofile":

http://www.gammon.com.au/scripts/doc.php?lua=dofile

Or, "loadfile":

http://www.gammon.com.au/scripts/doc.php?lua=loadfile

You can also write your own package loader, as described in the following links:

http://www.playwithlua.com/?p=11

http://sahd.lamafam.org/?p=1631

In case the links go down, the general idea would be to:


local path = "/whatever/my/path/is"

local function loader (modulename)
    local filename = path..modulename..".lua"
    return assert(loadfile(filename)())
end

package.preload ['foo'] = loader
package.preload ['bar'] = loader


Now if you do this:


require "foo"
require "bar"


Then your own loader is called which uses whatever path you want.