Lua 需要完整路径名的函数 [英] Lua Require function on a full path name

查看:21
本文介绍了Lua 需要完整路径名的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个 lua 文件上调用 require ,它不会总是在同一个地方.我试图在完整路径名上调用 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:UsersMeMyLuaProjectfoo"

example changing require "foo" to require "C:UsersMeMyLuaProjectfoo"

但是当我将其切换为完整路径名时,它再也找不到了.所以我想知道您是否甚至可以在完整路径上调用 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?

推荐答案

将包含文件的目录添加到 package.path:

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 需要完整路径名的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆