How do I reload a module in an active Julia session after an edit?

后端 未结 5 1347
青春惊慌失措
青春惊慌失措 2020-12-04 16:07

2018 Update: Be sure to check all the responses, as the answer to this question has changed multiple times over the years. At the time of this update, the <

5条回答
  •  执笔经年
    2020-12-04 17:05

    Use the package Revise, e.g.

    Pkg.add("Revise") # do this only once
    
    include("src/my_module.jl")
    using Revise
    import my_module
    

    You may need to start this in a new REPL session. Notice the use of import instead of using, because using does not redefine the function in the Main module (as explained by @Maciek Leks and @waTeim).

    Other solutions: Two advantages of Revise.jl compared to workspace() are that (1) it is much faster, and (2) it is future-proof, as workspace() was deprecated in 0.7, as discussed in this GitHub issue:

    julia> VERSION
    v"0.7.0-DEV.3089"
    
    julia> workspace()
    ERROR: UndefVarError: workspace not defined
    

    and a GitHub contributor recommended Revise.jl:

    Should we add some mesage like "workspace is deprecated, check out Revise.jl instead"?

    Even in Julia 0.6.3, the three previous solutions of workspace(), import, and reload fail when a module called other modules, such as DataFrames. With all three methods, I got the same error when I called that module the second time in the same REPL:

    ERROR: LoadError: MethodError: all(::DataFrames.##58#59, ::Array{Any,1}) is ambiguous. Candidates: ...
    

    I also got many warning messages such as:

    WARNING: Method definition macroexpand(Module, ANY) in module Compat at /Users/mmorin/.julia/v0.6/Compat/src/Compat.jl:87 overwritten in module Compat at /Users/mmorin/.julia/v0.6/Compat/src/Compat.jl:87.
    

    Restarting the Julia session worked, but it was cumbersome. I found this issue in the Reexport package, with a similar error message:

    MethodError: all(::Reexport.##2#6, ::Array{Any,1}) is ambiguous.
    

    and followed the suggestion of one contributor:

    Does this happen without using workspace()? That function is notorious for interacting poorly with packages, which is partly why it was deprecated in 0.7.

提交回复
热议问题