How does Julia using behave on missing package?

吃可爱长大的小学妹 提交于 2020-01-04 13:44:48

问题


So what exactly does Julia do with the statement using Foo if you don't have package Foo installed? As I understood Julia starts searching JULIA_LOAD_PATH, but how?

At the root level of JULIA_LOAD_PATH there must be a directory named Foo.jl where the Foo part may be case insensitive and the .jl suffix is optional?

And within this Foo.jl directory there must be a source file name Foo.jl with a module Foo?


回答1:


using implicitly calls require which indirectly calls find_in_path:

function find_in_path(name::AbstractString, wd = pwd())
    isabspath(name) && return name
    base = name
    # this is why `.jl` suffix is optional
    if endswith(name,".jl")
        base = name[1:end-3]
    else
        name = string(base,".jl")
    end
    if wd !== nothing
        isfile(joinpath(wd,name)) && return joinpath(wd,name)
    end
    for prefix in [Pkg.dir(); LOAD_PATH]
        path = joinpath(prefix, name)
        isfile(path) && return abspath(path)
        path = joinpath(prefix, base, "src", name)
        isfile(path) && return abspath(path)
        path = joinpath(prefix, name, "src", name)
        isfile(path) && return abspath(path)
    end
    return nothing
end

The source code above shows that there is no additional manipulation on name, which means the Foo part should be case sensitive(Currently depend on the filesystem, see the comment below). And the directory name is unnecessary to be compatible with your file name, it can be anything as long as the directory is in your LOAD_PATH.



来源:https://stackoverflow.com/questions/40269430/how-does-julia-using-behave-on-missing-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!