Using and developing own modules in Julia 1.6

Viewed 64

I am having some difficulty using my own modules in my project. My approach is the following:

I start by creating a folder called MyProject and a folder src within the MyProject folder. Within MyProject/src, I generate an empty project by executing generate MyModule. So far, so good.

The content of MyProject/src/MyModule/src/MyModule.jl is

module MyModule
export greet, myfunc

greet() = print("Hello World!")

function myfunc()
    print("hi from MyModule")
    return nothing
end

end # module

Now, this is my module. I want to use this in MyProject, so I go back to MyProject and activate the environment: activate .

To my understanding, in order to use MyModule in MyProject, I have to include it using dev: dev src/MyModule. When I write st, I can see that MyProject now contains MyModule - so far, so good.

I now create a file playground.jl in the MyProject-folder containing the code below, which executes flawlessly:

using MyModule

MyModule.greet()
MyModule.myfunc()

Q1. Is this approach correct and professional, or am I missing something in my workflow?

Q2. When I make changes in MyModule.jl, I go to MyProject and run ]instantiate which compiles the changes, but tells me to Restart julia to access the new version. Is this really necessary, or is there a way to get the changes of MyModule.jl without having to restart? It seems a bit excessive and tedious that I have to restart Julia to test my changes.

1 Answers

A1. Yeah, that makes sense. Probably the nicest package for this kind of project is DrWatson.jl, which recommends (almost) exactly this approach..

A2. MyModule should get re-compiled automatically, when you do using MyModule, so you don't need to go to all the instantiate trouble.

Also useful is Revise.jl, for interactive work. You start with

using Revise
using MyModule

(Note that Revise must come before MyModule.) Then you do some interactive work, and find something you want to change with MyModule. You can just go in and change the files, then use the updated code without restarting your interactive session, or manually recompiling or anything. Revise just automatically figures out that anything that changed will need to be recompiled, and does it for you like magic.

Related