maasha
maasha

Reputation: 1995

How does Julia using behave on missing package?

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?

Upvotes: 2

Views: 155

Answers (1)

Gnimuc
Gnimuc

Reputation: 8566

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.

Upvotes: 4

Related Questions