无法从 Windows 中的 Atom 运行的 Lua 加载/需要文件
我正在尝试使用Atom运行LUA脚本。但是,当我尝试通过remire()命令加载文件时,它总是说它无法找到它们。这些文件都在同一文件夹中。例如,要加载utils.lua我尝试了
require 'utils'
require 'utils.lua'
require 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua'
require 'D:\\Users\\Mike\\Dropbox\\Lua Modeling\\utils.lua'
require 'D:/Users/Mike/Dropbox/Lua Modeling/utils.lua'
我遇到的错误,就像
Lua: D:\Users\Mike\Dropbox\Lua Modeling\main.lua:12: module 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua' not found:
no field package.preload['D:\Users\Mike\Dropbox\Lua Modeling\utils.lua']
no file '.\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua\init.lua'
no file 'D:\Program Files (x86)\Lua\5.1\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
一条消息在第一行中所说的错误,即“ d:\ users \ mike \ dropbox \ lua建模\ utils.lua”,即使这是完整的,即使这是完整的文件的路径。我在做什么错?
谢谢。
I'm trying to use Atom to run a Lua script. However, when I try to load files via the require() command, it always says it's unable to locate them. The files are all in the same folder. For example, to load utils.lua I have tried
require 'utils'
require 'utils.lua'
require 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua'
require 'D:\\Users\\Mike\\Dropbox\\Lua Modeling\\utils.lua'
require 'D:/Users/Mike/Dropbox/Lua Modeling/utils.lua'
I get errors like
Lua: D:\Users\Mike\Dropbox\Lua Modeling\main.lua:12: module 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua' not found:
no field package.preload['D:\Users\Mike\Dropbox\Lua Modeling\utils.lua']
no file '.\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua\init.lua'
no file 'D:\Program Files (x86)\Lua\5.1\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
The messages says on the first line that 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua' was not found, even though that is the full path of the file. What am I doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案
您应该能够通过使用以下代码加载
utils.lua
:通过从目录中启动
utils.lua
的目录中的程序。
要了解这里出了什么问题,了解
需要
的工作方式有所帮助。首先要做的是在模块路径中搜索模块。来自在LUA第8.1章中的编程>从您的错误消息来判断,您的lua路径似乎是以下内容:
使得更易于阅读,这是每种图案按线路断开的模式:
从此列表中,您可以看到,当调用需要
.lua
for You for Youlua for lua填充在其他文件路径中为您提供其他 单词,您应该只指定模块名称,例如:
现在,lua还需要知道utils.lua文件在哪里。最简单的方法是从
d:\ users \ mike \ dropbox \ lua建模
文件夹中运行程序。这意味着,当您运行需要(“ utils”)
时,lua会将第一个模式。 >,当它检查该路径时,它将在当前目录中找到utils.lua文件。
换句话说,这样的程序运行应该有效:
,
如果您不能(或不想)更改工作目录以运行程序,则可以使用
lua_path
环境变量 为了在需要
用来搜索模块的路径中添加新模式。这有一个小技巧。如果
lua_path
环境变量已经存在,则将您的项目的文件夹添加到其开始。如果不存在lua_path
,则将将;; ;;
添加到结尾,而Lua填充了默认路径。The short answer
You should be able to load
utils.lua
by using the following code:And by starting your program from the directory that
utils.lua
is in:The long answer
To understand what is going wrong here, it is helpful to know a little bit about how
require
works. The first thing that require does is to search for the module in the module path. From Programming in Lua chapter 8.1:Judging from your error message, your Lua path seems to be the following:
To make that easier to read, here are each the patterns separated by line breaks:
From this list you can see that when calling require
.lua
extension for youIn other words, you should just specify the module name, like this:
Now, Lua also needs to know where the utils.lua file is. The easiest way is to run your program from the
D:\Users\Mike\Dropbox\Lua Modeling
folder. This means that when you runrequire("utils")
, Lua will expand the first pattern.\?.lua
into.\utils.lua
, and when it checks that path it will find the utils.lua file in the current directory.In other words, running your program like this should work:
An alternative
If you can't (or don't want to) change your working directory to run the program, you can use the
LUA_PATH
environment variable to add new patterns to the path thatrequire
uses to search for modules.There is a slight trick to this. If the
LUA_PATH
environment variable already exists, then this will add your project's folder to the start of it. IfLUA_PATH
doesn't exist, this will add;;
to the end, which Lua fills in with the default path.