gpt4 book ai didi

julia - 让 Julia 在 LOAD_PATH 中查找文件

转载 作者:行者123 更新时间:2023-12-03 22:33:32 25 4
gpt4 key购买 nike

我想通过修改 LOAD_PATH 来帮助 Julia 找到我的 .jl 文件。多变的:

julia> readdir()
1-element Array{String,1}:
"test.jl"

shell> cmd /c type test.jl
# Test.jl
module Test
export f
f() = println("hi")
end

julia> push!(LOAD_PATH,pwd());

julia> import Test
ERROR: ArgumentError: Module Test not found in current path.
Run `Pkg.add("Test")` to install the Test package.
in require(::Symbol) at .\loading.jl:365

第一次调用 readdir()证明我的当前目录中有一个名为 test.jl 的文件。以下 shell 调用显示该文件包含一个名为 Test 的模块。下一次调用 push!(LOAD_PATH,pwd());将当前目录放入 LOAD_PATH .但即使当前目录位于 LOAD_PATH , Julia 仍然找不到 Test test.jl 中的模块。

怎么了?

最佳答案

错误是在谈论有关 require 的内容。 .正如文档所说:

Given the statement using Foo, the system looks for Foo within Main. If the module does not exist, the system attempts to require("Foo"), which typically results in loading code from an installed package. ... require is case-sensitive on all platforms, including those with case-insensitive filesystems like macOS and Windows.



原因很明确: require找不到名为 Test 的文件在 LOAD_PATH .所以我们需要使文件名与模块名匹配,但这只是一个约定,不是强制性的规则。如果有人错误运行 using test 会发生什么?
julia> push!(LOAD_PATH,pwd())
julia> using test
WARNING: requiring "test" in module "Main" did not define a corresponding module.
julia> whos()
Base 34427 KB Module
Core 12386 KB Module
Main 41296 KB Module
Test 1837 bytes Module

结果显示我们已经加载了文件 test.jl。和其中的模块( Test ),但实际上不是 using/import模块。这是一种受人尊敬的行为,因为我们使用了错误的模块名称,这也是 Julia 在警告中提示的原因。在这种情况下, using test相当于 include("test.jl") ,但我强烈建议您遵守约定,不要使用此行为。

顺便说一句, requirethis PR 之后变得一般区分大小写.副作用是您的 LOAD_PATH也应该区分大小写,这将由 this PR 修复在 julia-0.6 中。

关于julia - 让 Julia 在 LOAD_PATH 中查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com