0
votes

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.

Update

Ok, maybe I was a bit quick with my post on Stackoverflow. I made some experiments and now for me I discovered:

  • If the using is avoided it works much more reliable.
  • Then includet as well as include works
  • Especially constants are a problem and cannot be redefined once in global namespace (?)
2
Can you specify, what exactly didn't work when you were using includet? Also, what do you mean by "it seems impossible to reload code without restarting the REPL"? What is happening, when you are changing contents of file1.jl and execute include("file1.jl") again?Andrej Oskin

2 Answers

0
votes

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.

0
votes

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".