无法从 Windows 中的 Atom 运行的 Lua 加载/需要文件

发布于 2025-01-20 06:31:02 字数 1036 浏览 5 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2025-01-27 06:31:02

简短的答案

您应该能够通过使用以下代码加载utils.lua

require("utils")

通过从目录中启动utils.lua的目录中的程序

cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua

要了解这里出了什么问题,了解需要的工作方式有所帮助。首先要做的是在模块路径中搜索模块。来自在LUA第8.1章中的编程>

要求使用的路径与典型路径有些不同。大多数程序使用路径作为目录列表,其中用于搜索给定文件。但是,ANSI C(LUA运行的抽象平台)没有目录的概念。因此,使用的路径require模式的列表,每个列表都指定了一种将虚拟文件名(需要需要的参数)转换为真实文件的替代方法姓名。更具体地说,路径中的每个组件都是包含可选询问标记的文件名。对于每个组件,需要用虚拟文件名替换每个,并检查是否有一个名称的文件;如果没有,它将转到下一个组件。路径中的组件被分号(大多数操作系统中用于文件名的字符很少)。例如,如果路径为

 ?;?
 

然后呼叫需要“ lili”尝试打开以下文件:

 莉莉
lili.lua
C:\ Windows \ Lili
/USR/local/lua/lili/lili.lua
 

从您的错误消息来判断,您的lua路径似乎是以下内容:

.\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?\init.lua;D:\Program Files (x86)\Lua\5.1\?.lua

使得更易于阅读,这是每种图案按线路断开的模式:

.\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?\init.lua
D:\Program Files (x86)\Lua\5.1\?.lua

从此列表中,您可以看到,当调用需要

  1. LUA填充时,.lua for You for You
  2. 文件路径

lua for lua填充在其他文件路径中为您提供其他 单词,您应该只指定模块名称,例如:

require("utils") 

现在,lua还需要知道utils.lua文件在哪里。最简单的方法是从d:\ users \ mike \ dropbox \ lua建模文件夹中运行程序。这意味着,当您运行需要(“ utils”)时,lua会将第一个模式。 >,当它检查该路径时,它将在当前目录中找到utils.lua文件。

换句话说,这样的程序运行应该有效:

cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua

如果您不能(或不想)更改工作目录以运行程序,则可以使用lua_path环境变量 为了在需要用来搜索模块的路径中添加新模式。

set LUA_PATH=D:\Users\Mike\Dropbox\Lua Modeling\?.lua;%LUA_PATH%;
lua "D:\Users\Mike\Dropbox\Lua Modeling\main.lua"

这有一个小技巧。如果lua_path环境变量已经存在,则将您的项目的文件夹添加到其开始。如果不存在lua_path,则将将;; ;;添加到结尾,而Lua填充了默认路径。

The short answer

You should be able to load utils.lua by using the following code:

require("utils")

And by starting your program from the directory that utils.lua is in:

cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua

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:

The path used by require is a little different from typical paths. Most programs use paths as a list of directories wherein to search for a given file. However, ANSI C (the abstract platform where Lua runs) does not have the concept of directories. Therefore, the path used by require is a list of patterns, each of them specifying an alternative way to transform a virtual file name (the argument to require) into a real file name. More specifically, each component in the path is a file name containing optional interrogation marks. For each component, require replaces each ? by the virtual file name and checks whether there is a file with that name; if not, it goes to the next component. The components in a path are separated by semicolons (a character seldom used for file names in most operating systems). For instance, if the path is

?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua

then the call require"lili" will try to open the following files:

lili
lili.lua
c:\windows\lili
/usr/local/lua/lili/lili.lua

Judging from your error message, your Lua path seems to be the following:

.\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?\init.lua;D:\Program Files (x86)\Lua\5.1\?.lua

To make that easier to read, here are each the patterns separated by line breaks:

.\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?\init.lua
D:\Program Files (x86)\Lua\5.1\?.lua

From this list you can see that when calling require

  1. Lua fills in the .lua extension for you
  2. Lua fills in the rest of the file path for you

In other words, you should just specify the module name, like this:

require("utils") 

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 run require("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:

cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua

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 that require uses to search for modules.

set LUA_PATH=D:\Users\Mike\Dropbox\Lua Modeling\?.lua;%LUA_PATH%;
lua "D:\Users\Mike\Dropbox\Lua Modeling\main.lua"

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. If LUA_PATH doesn't exist, this will add ;; to the end, which Lua fills in with the default path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文