I have a module I wrote here:
# Hello.jl module Hello function foo return 1 end end
and
# Main.jl using Hello foo()
When I run the Main
module:
$ julia ./Main.jl
I get this error:
ERROR: LoadError: ArgumentError: Hello not found in path in require at ./loading.jl:249 in include at ./boot.jl:261 in include_from_node1 at ./loading.jl:320 in process_options at ./client.jl:280 in _start at ./client.jl:378 while loading /Main.jl, in expression starting on line 1
Installing modulesEdit To download and install a package, you can use Julia's package manager, Pkg. Start by typing a right bracket ] into the REPL to enter the package manager, then use the add command. You don't have to use quotes or the ". jl" suffix.
To load a module from a package, the statement using ModuleName can be used. To load a module from a locally defined module, a dot needs to be added before the module name like using . ModuleName . would load the above code, making NiceStuff (the module name), DOG and nice available.
My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With