Contents

Julia module loading

When the code grows to a certain size and you want to separte the common parts.

Reference

Assuming we have the file structure for the submodules

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
. present working directory (pwd)
| - main.jl
| - Manifest.toml
| - Project.toml
|
+---Mod1.jl
|   | - Manifest.toml
|   | - Project.toml
|   |
|   \---src
|         - Mod1.jl
|
\---Mod2.jl
    | - Manifest.toml
    | - Project.toml
    |
    \---src
          - Mod2.jl

Submodules: include code

You could include the little packages as submodules like this

1
2
3
4
5
include("./Mod1.jl/src/Mod1.jl")
using .Mod1

include("./Mod2.jl/src/Mod2.jl")
using .Mod2
  • Best when the submodules are used exclusively for this project.
  • Usually you want to include() all dependent files in the top-most file, just like a table of contents.
  • include and using lines are needed to be executed again when the code in Modx changes.
  • Use relative module path when Mod2 also requires Mod1.
  • There might be recursive include() calls and replace module warnings.

dev --local pkg...

Julia docs | Pkg | dev

Add local packages and track the file changes in the Julia REPL

1
2
3
julia> ]
pkg> activate .
pkg> dev --local Mod1 Mod2

Or by the Julia script counterpart

1
2
3
4
5
6
7
import Pkg

# To generate Project.toml if not present
Pkg.activate(".")

Pkg.develop(PackageSpec(path="Mod1.jl"))
Pkg.develop(PackageSpec(path="Mod2.jl"))
  • Best when Mod1 and Mod2 are modified frequently and shared.
  • Loaded code is determined by local files instead of package versions.
  • The updates are loaded when using is invoked, along with precompilation. Revise.jl tracks and updates modified files and you don’t have to restart the Julia process should you make changes.

Hosted from your repo

Make a Git repo for your custom module and publish it to Git service providers, e.g. GitHub / Gitlab.

And then you can: ]add https://github.com/username/Mod1.jl.git

PkgTemplates.jl is recommended to generate package with unit tests and CI/CD settings.

Nonetheless, it’s just one step away from proper registeration to the general Julia registry to used by more people.

Appending LOAD_PATH (⚠️ outdated)

1
2
3
4
push!(LOAD_PATH, ".")

using Mod1
using Mod2
  • The old way before Pkg3 and Julia 1.0, which is not recommended by now.
  • No need for Project.toml nor Manifest.toml in the pwd.
  • Local files are tracked instead of package versions.
  • VS Code language server could not identify symbols from the custom modules.