How to 'using a_local_module'

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

myModule.jl. contains
module myModule

end

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


using myModule
and
import myModule

both are for modules already registered by Pkg.


thanks
HP

The simplest thing is:

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

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.

2 Likes

A third option is

include("myModule.jl")
using .myModule # <-- the . is important!
2 Likes

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