How to 'using a_local_module'

In a project directory,
I have a main.jl and a myModule.jl

myModule.jl. contains
module myModule


In the main.jl,
how do I use myModule ?

using myModule
import myModule

both are for modules already registered by Pkg.


The simplest thing is:

julia> push!(LOAD_PATH, ".")
4-element Array{String,1}:

julia> using myModule

By pushing to LOAD_PATH, you tell Julia to also look in the current folder (".") when loading modules.

For a slightly more advanced version, try this:

  • Run ]generate myModule (pressing the ] key to get the pkg> prompt in Julia)
    • This will create a new src folder and a new src/myModule.jl file
    • it will also create a Project.toml in your current folder
  • Copy your module into src/myModule.jl
  • Run julia --project
    • This will look at the Project.toml that you just generated

You should then be able to do using myModule without any modification of LOAD_PATH, as long as you start julia with julia --project

The advantage of this approach is that you now have a brand new clean environment for adding any new packages that you might need for working on myModule, and you can install packages into that environment without messing up the rest of your system.


A third option is

using .myModule # <-- the . is important!

In case an explanation helps someone:
using .myModule
is AFAIK equivalent to
using CurrentModule.myModule.

I want to emphasize this more. You should always use ] generate to use a local module. Just creating the right folder structure won’t make you able to do using when it is local.

1 Like