Lua Require函数的完整路径名 [英] Lua Require function on a full path name
问题描述
我需要在lua文件上调用require,而lua文件不会总是在同一位置.我试图在完整路径名上调用require,但这似乎也不起作用.我什至尝试用正确的完整路径名替换同一文件的正常工作要求
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 either. I even tried replacing one of my working normal requires with a correct full path name to the same file
示例更改 需要"foo" 到 需要"C:\ Users \ Me \ MyLuaProject \ foo"
example changing require "foo" to require "C:\Users\Me\MyLuaProject\foo"
但是当我将其切换为完整路径名时,它不再能找到它.因此,我想知道您是否甚至可以在完整路径上调用require,如果不能,我将如何以不同的方式实现相同的结果?
but when i switched it to the full path name it could no longer find it. So I am wondering if you can even call require on a full path and if not how would i achieve the same result differently?
推荐答案
Add the directory containing the file to package.path:
package.path = package.path .. ";C:\\Users\\Me\\MyLuaProject"
require "foo"
您也可以将其添加到LUA_PATH环境变量中,但这可能不太容易即时修改.
You can also add it to the LUA_PATH environment variable, but this is probably less easy to modify on the fly.
模块的常见模式是具有abc.lua和abc/xyz.lua;要在这样的子目录中需要文件,请使用以下命令:
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"
这篇关于Lua Require函数的完整路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!