Lua Require function on a full path name

后端 未结 2 383
天命终不由人
天命终不由人 2021-01-11 13:08

I need to call the require on a lua file that will not always be in the same place. I was trying to call require on the full path name but that doesn\'t seem to be working e

相关标签:
2条回答
  • 2021-01-11 13:35

    If you just need to load a file, use dofile, which takes a path:

    dofile("C:\\Users\\Me\\MyLuaProject\\foo")
    
    0 讨论(0)
  • 2021-01-11 13:50

    Add the directory containing the file to package.path:

    package.path = package.path .. ";C:\\Users\\Me\\MyLuaProject"
    require "foo"
    

    You can also add it to the LUA_PATH environment variable, but this is probably less easy to modify on the fly.

    A common pattern for modules is to have abc.lua and abc/xyz.lua; to require files in a subdirectory like that, use the following:

    require "abc"
    require "abc.xyz"
    
    0 讨论(0)
提交回复
热议问题