Assume that your file is named foo.lua
, then in the Lua interpreter (i.e, the interactive mode), use loadfile
. Note that loadfile
does not raise error, so it's better to use assert
with it.
f = assert(loadfile("foo.lua"))
It will load the chunk in foo.lua
into the function f
. Note that this will only load the chunk, not run it. To run it, call the function:
f()
If you need to run it immediately, you can use dofile
:
dofile("foo.lua")
Lua uses package.path
as the search path which gets its default value from LUA_PATH
. However, it's better to use proper relative path in practice.