Understanding the startup.jl file -- is there a environment-specific version?

Say your project environment is called MyProject. Create a subfolder named src and put MyProject.jl in it (basically, you end up with path/to/MyProject/src/MyProject.jl). Now, in this file you create a module exactly named MyProject and put your startup code inside the __init__() function:

module MyProject

function __init__()
    #stuff to do at startup
end

end

In every script of your MyProject environment, you can call using MyProject at the beginning: this will execute whatever you put inside the __init__() function in the MyProject module. It has also the added benefit of being folder independent (you simply call using MyProject without caring about what folder your script is currently in) and you make sure that whatever you want to be executed at startup is called exactly once in the lifetime of your REPL session. In fact, you can include scripts inside other scripts, each of which have using MyProject at the beginning, and there won’t be any re-execution of the startup code (which instead would happen if your startup code is included in every script). Of course, you can also define and export any function you like in the MyProject module, and it will be instantly available when calling using MyProject. I think this is the easiest and most flexible solution, without an actual startup.jl file. Also, it can be useful having this code not being automatically loaded, in case you want to debug without having it loaded inside your environment.

5 Likes