Julia Modules Guide — Module System, Exports, and Package Organization
In this tutorial, you will learn about Julia Modules Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia modules encapsulate definitions with export (public API), import/using (dependency management), and include (file loading) -- supporting relative names (MyModule.func), nested modules, and separate compilation for package organization.
Defining a Module
module MyUtils
export trim, slugify, capitalize
function trim(s::String)
return strip(s)
end
function slugify(s::String)
s = lowercase(s)
s = replace(s, r"[^a-z0-9]+" => "-")
s = strip(s, '-')
return s
end
end # module
Using Modules
# Load module file
include("MyUtils.jl")
using .MyUtils
# Now use exported functions
trim(" hello ") # "hello"
slugify("Hello World!") # "hello-world"
# Non-exported names need prefix
MyUtils.capitalize("hello")
# Import specific names
import .MyUtils: trim
Module Files
# File: Geometry.jl
module Geometry
include("Geometry/Circle.jl")
include("Geometry/Rectangle.jl")
export Circle, Rectangle, area, perimeter
end
# File: Geometry/Circle.jl
module Circle
export area, circumference
area(r) = π * r^2
circumference(r) = 2π * r
end
Packages
# Using registered packages
using Pkg
Pkg.add("Plots")
using Plots
# Using unregistered package
Pkg.add(url="https://github.com/user/Package.jl")
# Project.toml
# [deps]
# Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
# Manifest.toml (auto-generated)
Common Mistakes
1. Circular module dependencies
Module A using B and B using A causes load errors. Refactor shared code into a third module.
2. Missing export
Functions without export need qualified access. Export keeps the public API manageable.
3. include vs using confusion
include() loads source files (like copy-paste). using/import load compiled modules.
Practice Questions
1. How do you make a function public from a module?
Use export function_name within the module. Exported functions are available with using ModuleName.
2. What is the difference between using and import?
using M makes exported names available directly. import M requires M.name. import M: name imports specific names.
3. How do you add a package?
using Pkg; Pkg.add("PackageName") downloads and installs from the Julia package registry.
FAQ
{{< faq question="What is Project.toml?" >}} The package descriptor file. It lists name, UUID, version, dependencies, and compat entries. {{< /faq >}}
{{< faq question="Can I have submodules?" >}}
Yes. Define nested module Sub inside module Main, or use separate files with include.
{{< /faq >}}
{{< faq question="What is the Main module?" >}} The top-level module where the REPL and scripts run. All user-defined functions go into Main by default. {{< /faq >}}
What's Next
Now learn about file I/O in Julia.
| Topic | Description | Link |
|---|---|---|
| File I/O | Reading and writing files | {{< ref "12-file-io" >}} |
| Plotting | Data visualization | {{< ref "13-plotting" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro