How to import custom module in julia

后端 未结 7 2181
抹茶落季
抹茶落季 2020-12-08 19:35

I have a module I wrote here:

# Hello.jl
module Hello
    function foo
        return 1
    end
end

and

# Main.jl
using Hel         


        
7条回答
  •  余生分开走
    2020-12-08 19:52

    There is a new answer to this question since the release of Julia v0.7 and v1.0 that is slightly different. I just had to do this so I figured I'd post my findings here.

    As already explained in other solutions, it is necessary to include the relevant script which defines the module. However, since the custom module is not a package, it cannot be loaded as a package with the same using or import commands as could be done in older Julia versions.

    So the Main.jl script would be written with a relative import like this:

    include("./Hello.jl")
    using .Hello
    foo()
    

    I found this explained simply in Stefan Karpinski's discourse comment on a similar question. As he describes, the situation can also get more elaborate when dealing with submodules. The documentation section on module paths is also a good reference.

提交回复
热议问题