Reputation: 41
Sorry for the basic beginner question but I'm really struggling with a problem concerning Julia and it's REPL.
I want to use the "MATLAB like" workflow of trying and coding at the same time. I just want to split code/functions among multiple files and combine them in a script:
include("file1.jl") # defines Module1
include("file2.jl") # defines Module2
using .Module1
using .Module2
But somehow it seems to be impossible to reload code defined in those modules without restarting the REPL. I tried using Revise.jl
and the "tracking include" includet
but it doesn't work for me.
Does someone had a similar problem and found a working solution?
This problems seems to occur frequently (e.g. here) and it's annoyance is amplified by the slow startup time / recompilation time of Julia.
Ok, maybe I was a bit quick with my post on Stackoverflow. I made some experiments and now for me I discovered:
using
is avoided it works much more reliable.includet
as well as include
worksUpvotes: 0
Views: 752
Reputation: 626
Tasos Papastylianou's answer works. If you have a module say, called Mymod, file name is Mymod.jl. At first you do a
include("Mymod.jl")
using Mymod
Then you changed Mymod.jl a little, to reload Mymod to reflect the changes, you simply do the include("Mymod.jl") and using Mymod again, I mean just repeat
include("Mymod.jl")
using Mymod
This time the Mymod is reloaded, and you will see the changes you made. At least it works for Julia 1.6.1. You do not have to use packages like "Revise".
Upvotes: 2
Reputation: 22225
If you just keep function definitions in your files, then these should get overwritten / redefined when you include the file again.
If your files are modules, you don't need to do using
to use it. When you include the file, the module will get "defined" locally as if you had typed it in your REPL. Just use its functionality with the module name as an identifier. If you change the module in your file and 'include' it again, julia will warn you that it's replacing the module with a new one of the same name, and that's that.
There are a couple of things to note regarding the use of 'include' within scripts, to do with World Number etc ... but in the context that you are talking about, which is replicating matlab/octave workflow in the REPL, this should not be a problem.
Upvotes: 1