I have a module I wrote here:
# Hello.jl
module Hello
function foo
return 1
end
end
and
# Main.jl
using Hel
You should include("./Hello.jl")
before using Hello
Unless you explicitly load the file (include("./Hello.jl")
) Julia looks for module files in directories defined in the LOAD_PATH variable.
See this page.
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 want to access function foo when importing the module with "using" you need to add "export foo" in the header of the module.
I have Julia Version 1.4.2 (2020-05-23)
. Just this using .Hello
worked for me.
However, I had to compile the Hello module before just using .Hello
. It makes sense for both the defined and using scripts of Hello
is on the same file.
Instead, we can define Hello
in one file and use it in a different file with include("./Hello.jl");using .Hello
This answers was originally written for Julia 0.4.5. There is now an easier way of importing a local file (see @kiliantics answer). However, I will leave this up as my answer explains several other methods of loading files from other directories which may be of use still.
There have already been some short answers, but I wanted to provide a more complete answer if possible.
When you run using MyModule
, Julia only searches for it in a list of directories known as your LOAD_PATH
. If you type LOAD_PATH
in the Julia REPL, you will get something like the following:
2-element Array{ByteString,1}:
"/Applications/Julia-0.4.5.app/Contents/Resources/julia/local/share/julia/site/v0.4"
"/Applications/Julia-0.4.5.app/Contents/Resources/julia/share/julia/site/v0.4"
These are the directories that Julia will search for modules to include when you type using Hello
. In the example that you provided, since Hello
was not in your LOAD_PATH
, Julia was unable to find it.
If you wish to include a local module, you can specify its location relative to your current working directory.
julia> include("./src/Hello.jl")
Once the file has been included, you can then run using Hello
as normal to get all of the same behavior. For one off scripts, this is probably the best solution. However, if you find yourself regular having to include()
a certain set of directories, you can permanently add them to your LOAD_PATH
.
Adding directories to LOAD_PATH
Manually adding directories to your LOAD_PATH
can be a pain if you wish to regularly use particular modules that are stored outside of the Julia LOAD_PATH
. In that case, you can append additional directories to the LOAD_PATH
environment variable. Julia will then automatically search through these directories whenever you issue an import
or using
command.
One way to do this is to add the following to your .basrc
, .profile
, .zshrc
.
export JULIA_LOAD_PATH="/path/to/module/storage/folder"
This will append that directory onto the standard directories that Julia will search. If you then run
julia> LOAD_PATH
It should return
3-element Array{ByteString,1}:
"/path/to/module/storage/folder"
"/Applications/Julia-0.4.5.app/Contents/Resources/julia/local/share/julia/site/v0.4"
"/Applications/Julia-0.4.5.app/Contents/Resources/julia/share/julia/site/v0.4"
You can now freely run using Hello
and Julia will automatically find the module (as long as it is stored underneath /path/to/module/storage/folder
.
For more information, take a look at this page from the Julia Docs.