Julia Modules
A module is a separate namespace: a container for functions, types and globals that keeps their names from clashing with code elsewhere. Every package is a module, and the standard library modules you've met — Statistics, Printf, LinearAlgebra — are too.
using and import
| Statement | Effect |
|---|---|
using Statistics |
loads the module and brings all its exported names into scope (mean, std, ...) |
using Statistics: mean, median |
brings only the listed names into scope |
import Statistics |
loads the module; you write Statistics.mean |
import Statistics as St |
same, with a shorter alias |
import Base: show |
needed to add methods to show without writing Base.show |
using Statistics: mean, median
import LinearAlgebra as LA
import Printf
data = [3, 1, 4, 1, 5, 9]
println(mean(data), " ", median(data))
println(LA.norm([3, 4]))
Printf.@printf("%.3f\n", LA.dot([1, 2], [3, 4]) / 7)
println(isdefined(Main, :std)) # std was not imported3.8333333333333335 3.5
5.0
1.571
false
Listing names explicitly makes it obvious where each function comes from, which is good practice in larger programs.
Defining a module
Wrap code in module Name ... end. Inside, export declares the public names that using will bring in:
module Geometry
export Circle, area
const UNIT = "cm"
struct Circle
r::Float64
end
area(c::Circle) = π * c.r^2
circumference(c::Circle) = 2π * c.r # not exported
describe(c::Circle) = "circle of radius $(c.r) $UNIT"
end # module
using .Geometry # the dot means "a module defined in the current module"
c = Circle(2)
println(round(area(c), digits=3))
println(round(Geometry.circumference(c), digits=3))
println(Geometry.describe(c))
println(Geometry.UNIT)12.566
12.566
circle of radius 2.0 cm
cm
Non-exported names are still reachable with the qualified form Geometry.circumference. Julia has no truly private names; export documents the intended public API. Since Julia 1.11 you can also mark names as public without exporting them.
Modules as namespaces
Two modules can define functions with the same name without conflict:
module English
greet(name) = "Hello, $name"
end
module Spanish
greet(name) = "Hola, $name"
end
println(English.greet("Ana"))
println(Spanish.greet("Ana"))
println(English.greet === Spanish.greet)
println(English)Hello, Ana
Hola, Ana
false
Main.English
The two greet functions are completely separate objects. A module prints with its full path: code in a script or the REPL lives in the module Main, so English is Main.English.
Nested modules and relative paths
Modules can contain modules. Inside a module, ..Name refers to a sibling in the parent module:
module App
module Utils
shout(s) = uppercase(s) * "!"
end
module Views
using ..Utils: shout
page(title) = "<h1>" * shout(title) * "</h1>"
end
end
println(App.Views.page("welcome"))
println(App.Utils.shout("hi"))<h1>WELCOME!</h1>
HI!
Modules in files
Real projects split code across files. A typical package has a main file that defines the module and includes the rest:
# src/Shapes.jl
module Shapes
export Circle, Square, area
include("circle.jl") # pastes the file's code here
include("square.jl")
end# src/circle.jl
struct Circle
r::Float64
end
area(c::Circle) = π * c.r^2Packages created with Pkg.generate("Shapes") follow this layout, and using Shapes then works from any project that has the package installed (see the Package Manager lesson).
Base and Core
Every module automatically imports Base, which is why println, sum and + are always available. To add a method to a Base function, either qualify it (Base.show(io::IO, x::MyType) = ...) or import it first (import Base: show). Defining show(...) without either creates a new, unrelated function in your module.
Test yourself
After import Statistics, how do you call mean?
- mean(x)
- Statistics.mean(x)
- Statistics::mean(x)
- import mean first
What does export do inside a module?
- Makes the name the only accessible one
- Marks names that
usingbrings into the caller's scope - Saves the module to a file
- Compiles the module
What does the dot mean in using .Geometry?
- A typo
- Geometry is a module defined inside the current module, not an installed package
- Import everything including private names
- Load the module lazily