Julia code loading
The documentation about Julia code loading: https://docs.julialang.org/en/v1/manual/code-loading/
See also Julia package management
Include other files as submodules
You could include julia files as submodules like this
## main.jl
include("foo.jl")
using .Foo
include("bar.jl")
using .Bar## foo.jl
module Foo
# content of Foo module
end## bar.jl
module Bar
# content of Bar module
end- Best when the submodules are used exclusively for this project and will not be shared with others.
- Usually you want to include all dependent submodules in the top-most file, like a table of contents.
- The
includeandusinglines need to be re-execute when the code in the submodule changes. (ifRevise.includet("foo.jl")is not used) - Use relative module path when
Bardepends onFoo.
FromFile.jl
FromFile.jl provides a macro @from to replace include(). Using @from to access a file multiple times (for example calling @from “file.jl” import foo in multiple files) will access the same objects each time; i.e. without the duplication issues that include("file.jl") would introduce.
# file1.jl
import FromFile: @from
@from "file2.jl" import foo
bar() = foo()#file2.jl
foo() = println("hi")Code loading in a Package
To create a minimal package in the REPL (package mode)
pkg> generate JuliaHelloIn this example, main.jl has access to the JuliaHello module, because Julia sees src/JuliaHello.jl as an entry point for the JuliaHello package / module.
## src/JuliaHello.jl
module JuliaHello
greet() = print("Hello World!")
end # module## main.jl
using JuliaHello
JuliaHello.greet()“Developing” a temporary package
Use the Julia Pkg command dev --local pkg...
Assuming we have the file structure for the packages
. present working directory (pwd)
| - main.jl
| - Manifest.toml
| - Project.toml
|
+---Mod1.jl
| | - Manifest.toml (optional)
| | - Project.toml
| |
| \---src
| - Mod1.jl
|
\---Mod2.jl
| - Manifest.toml (optional)
| - Project.toml
|
\---src
- Mod2.jlAdd local packages and track the file changes in the Julia REPL
julia> ]
pkg> activate .
pkg> dev --local Mod1 Mod2Or run the commands in the Julia script
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
Mod1andMod2are modified frequently and shared. - Loaded code is determined by local files instead of package versions.
- The updates are loaded when
usingis invoked, along with precompilation. Revise.jl tracks and updates modified files and you don’t have to restart the Julia process upon module code changes.