Lua要求不工作 [英] Lua require not working

查看:102
本文介绍了Lua要求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使一个lua文件需要另一个.我正在遵循此指南: http://lua-users.org/wiki/ModulesTutorial

I am trying to make one lua file require another. I am following this guide: http://lua-users.org/wiki/ModulesTutorial

我的基本测试应该是平凡的打招呼"世界,但无法正常工作,我不知道为什么.

My basic test, which should be a trivial hello world, does not work, and I can't figure out why.

这是一个控制台日志,其中显示了所有文件和所有错误:

Here's a console log which shows all files and all errors:

C:\Users\TestUser\Desktop\LuaTest>dir
 Volume in drive C has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Users\TestUser\Desktop\LuaTest

11/15/2017  03:03 PM    <DIR>          .
11/15/2017  03:03 PM    <DIR>          ..
11/15/2017  02:53 PM    <DIR>          Bar
11/15/2017  03:04 PM                92 BazModule.lua
11/15/2017  02:53 PM    <DIR>          Foo
11/15/2017  03:08 PM               139 main.lua
               2 File(s)            231 bytes
               4 Dir(s)  253,774,073,856 bytes free

C:\Users\TestUser\Desktop\LuaTest>lua main.lua
lua: main.lua:1: module 'BazModule' not found:
        no field package.preload['BazModule']
        no file 'C:\dev\LuaDist\bin'
        no file '.\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
stack traceback:
        [C]: in function 'require'
        main.lua:1: in main chunk
        [C]: ?

C:\Users\TestUser\Desktop\LuaTest>type main.lua
local baz = require("BazModule")
baz.Baz()

local bar = require("Bar.BarModule")
bar.Bar()

local foo = require("Foo.FooModule")
foo.Foo()

C:\Users\TestUser\Desktop\LuaTest>type BazModule.lua
local BazModule = {}

function BazModule.Baz()
    print("Hello Baz!")
end

return BazModule

C:\Users\TestUser\Desktop\LuaTest>lua -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio

预期输出应为

Hello Baz!
Hello Bar!
Hello Foo!

但是它找不到与main.lua相邻的任何文件,我也不明白为什么.

But it can't find any of the files adjacent to main.lua and I don't understand why.

推荐答案

require package.path (用于Lua文件)和package.cpath(用于编译的库).

require searches in directories listed in package.path (for Lua files) and package.cpath (for compiled libraries).

您的错误消息...

lua: main.lua:1: module 'BazModule' not found:
        no field package.preload['BazModule']
        no file 'C:\dev\LuaDist\bin'
        no file '.\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'

指示require搜索的路径.看来package.path完全空的,或者其中可能只有一个格式错误的路径模式. (这将是C:\dev\LuaDist\bin.)

indicates the paths that require searched in. It seems that package.path is completely empty, or maybe there's a single malformed path pattern in there. (Which would be C:\dev\LuaDist\bin.)

搜索模块foo.bar的方法有效?foo/bar(或foo\bar –取决于操作系统)所替代,因此./?.lua会找到./foo/bar.lua.

The way the search for a module foo.bar works is that ? is substituted by foo/bar (or foo\bar – depending on OS) and so ./?.lua would find ./foo/bar.lua.

因此解决此问题的方法是(a)修复您(或您安装的东西)正在/正在操纵package.path的位置(

So the way to fix this is to (a) fix the place where you (or something that you installed) are/is mangling the package.path (via environment variable, startup script, …?) and/or (b) add the current directory to the search path.

这篇关于Lua要求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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