How do I reload a module under development in Julia 1.6?

I know this question has been asked and answered before, but none of the many answers work for me as described.

What is the procedure for reloading a module that I’m working on in Julia (1.6)?

For example, I have

module MyModule

export letters

const letters = String('A':'Z')

end

and I want the be able to load the module, make changes to letters in the module’s file, and then reload the module and have those changes reflected in subsequent uses of letters. This seems simple enough, but I can’t get it to work.

I’ve tried

include("src/MyModule.jl")
using .MyModule

but if I change the definition of letters in MyModule.jl and then

include("src/MyModule.jl")

letters doesn’t change, unless I fully qualify its use each time with Main.MyModule.letters: using Main.MyModule; letters refers, for example, to the old definition.

How do I reload a module under development so that I can refer to its definitions without fully qualifying them (and without having an unqualified shadow definition always lying around)?

1 Like

I highly recommend looking at the Revise based workflow which will reload the file for you:
https://docs.julialang.org/en/v1/manual/workflow-tips/#Revise-based-workflows

Also the standard place to put modules in development is your .julia/dev/ folder. If you would like to use another folder, you will need to modify your LOAD_PATH or your JULIA_LOAD_PATH environmental variable.

You would then load your module via using MyModule.

2 Likes

Here’s a demonstration:

julia> pwd()
"C:\\Users\\kittisopikulm\\Documents\\Julia\\ReviseDemo"

julia> push!(LOAD_PATH, pwd())
4-element Vector{String}:
 "@"
 "@v#.#"
 "@stdlib"
 "C:\\Users\\kittisopikulm\\Documents\\Julia\\ReviseDemo"

julia> using Revise

julia> open("ReviseDemo.jl","w") do io
           write(io,
           """
           module ReviseDemo
           export letters
           const letters = String('A':'C')
           end
           """
           )
       end
69

julia> using ReviseDemo
[ Info: Precompiling ReviseDemo [top-level]

julia> letters
"ABC"

julia> open("ReviseDemo.jl","w") do io
           write(io,
           """
           module ReviseDemo
           export letters
           const letters = String('A':'Z')
           end
           """
           )
       end
69

julia> letters
WARNING: redefinition of constant letters. This may fail, cause incorrect answers, or produce other errors.
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Here’s another demo setting the environmental variable in Windows:

PS C:\Users\kittisopikulm\Documents\Julia\ReviseDemo> $env:JULIA_LOAD_PATH="$(pwd);"
PS C:\Users\kittisopikulm\Documents\Julia\ReviseDemo> julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.0 (2021-03-24)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> LOAD_PATH
4-element Vector{String}:
 "C:\\Users\\kittisopikulm\\Documents\\Julia\\ReviseDemo"
 "@"
 "@v#.#"
 "@stdlib"

julia> using Revise

julia> open("ReviseDemo.jl","w") do io
           write(io,
           """
           module ReviseDemo
           export letters
           letters = String('A':'Z')
           end
           """
           )
       end
63

julia> using ReviseDemo
[ Info: Precompiling ReviseDemo [top-level]

julia> letters
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

julia> open("ReviseDemo.jl","w") do io
           write(io,
           """
           module ReviseDemo
           export letters
           letters = String('A':'C')
           end
           """
           )
       end
63

julia> letters
"ABC"

Note if you are using Linux, then you will want to use a colon, :, rather than a semicolon ; at the end of the JULIA_LOAD_PATH environmental variable.