Lua Require function on a full path name

左心房为你撑大大i 提交于 2019-12-19 05:17:21

问题


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

example changing require "foo" to require "C:\Users\Me\MyLuaProject\foo"

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?


回答1:


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"



回答2:


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

dofile("C:\\Users\\Me\\MyLuaProject\\foo")


来源:https://stackoverflow.com/questions/11868847/lua-require-function-on-a-full-path-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!