ZetCode

Julia Modules Tutorial

last modified November 18, 2026

In Julia, modules are used to organize code into separate namespaces. They help prevent naming conflicts and make code more modular and reusable. This tutorial covers basic definitions and practical examples of using modules in Julia. The examples were run with Julia 1.13.

A module is defined using the module keyword and closed with end. It can contain functions, types, and other modules. Modules are brought into scope with the using or import keywords; a leading dot, as in using .MyModule, refers to a module defined in the current file rather than an installed package.

Basic Module Definition

This example demonstrates how to define a basic module.

main.jl
module MyModule
    export greet
    greet() = println("Hello from MyModule!")
end

MyModule.greet()

The export keyword makes the greet function available when the module is brought into scope with using. Exporting is not required: the function can always be reached with the qualified name MyModule.greet, as we do here.

$ julia main.jl
Hello from MyModule!

Using a Module

This example shows how to use a module and its exported functions.

main.jl
module MyModule
    export greet
    greet() = println("Hello from MyModule!")
end

using .MyModule
greet()

The using keyword imports the MyModule module and makes its exported names available in the current namespace. That is why greet() can be called without the module prefix.

$ julia main.jl
Hello from MyModule!

Importing Specific Functions

This example demonstrates how to import specific functions from a module.

main.jl
module MyModule
    export greet
    greet() = println("Hello from MyModule!")
end

import .MyModule: greet
greet()

The import keyword with a colon imports only the greet function from MyModule. Unlike using, this form works for names that the module has not exported, and it allows the function to be extended with new methods.

$ julia main.jl
Hello from MyModule!

Nested Modules

This example shows how to define and use nested modules.

main.jl
module OuterModule
    module InnerModule
        export inner_greet
        inner_greet() = println("Hello from InnerModule!")
    end
end

using .OuterModule.InnerModule
inner_greet()

The InnerModule is nested inside OuterModule. Its qualified name is OuterModule.InnerModule, and a leading dot in the using statement brings that nested module into scope so that inner_greet() can be called directly.

$ julia main.jl
Hello from InnerModule!

Module Aliasing

This example demonstrates how to alias a module for easier access.

main.jl
module MyModule
    export greet
    greet() = println("Hello from MyModule!")
end

import .MyModule as MM
MM.greet()

Use import ... as to give a module a shorter local name. The form using .MyModule as MM shown in older examples is not valid Julia; with using, the as keyword can rename an individual name, for example using .MyModule: greet as hello.

$ julia main.jl
Hello from MyModule!

Module Precompilation

This example shows the precompilation directive in a module.

main.jl
module PrecompiledModule
    __precompile__(true)
    export precompiled_greet
    precompiled_greet() = println("Hello from PrecompiledModule!")
end

using .PrecompiledModule
precompiled_greet()

Julia precompiles package modules by default, so calling __precompile__(true) merely states that default and is harmless. The directive is meaningful with false, which opts a module out of precompilation. A module defined inline in a script, as here, is not written to a precompilation cache; the call simply shows the syntax.

$ julia main.jl
Hello from PrecompiledModule!

Module Documentation

This example demonstrates how to add documentation to a module.

main.jl
"""
    This is a documented module.
"""
module DocumentedModule
    export documented_greet
    documented_greet() = println("Hello from DocumentedModule!")
end

using .DocumentedModule
documented_greet()

The triple-quoted string is a docstring and must be placed immediately before the module keyword. Placing it inside the module, before an export, is an error because Julia would try to attach the documentation to the export statement. The text can be retrieved with @doc DocumentedModule.

$ julia main.jl
Hello from DocumentedModule!

Module Constants

This example shows how to define and use constants within a module.

main.jl
module ConstantsModule
    export PI
    const PI = 3.14159
end

using .ConstantsModule
println("The value of PI is $PI")

The const keyword defines the constant PI within the module. Because it is exported, using .ConstantsModule makes it available by its bare name. Constants cannot be reassigned after they are defined.

$ julia main.jl
The value of PI is 3.14159

Module Testing

This example demonstrates how to write tests for a module.

main.jl
using Test

module TestModule
    export add
    add(x, y) = x + y
end

using .TestModule

@testset "Addition" begin
    @test add(2, 3) == 5
end

The @test macro checks that add(2, 3) == 5. Wrapping it in a @testset gives a summary line at the end of the run; a bare @test outside a test set reports failures only and prints nothing when it passes.

$ julia main.jl
Test Summary: | Pass  Total  Time
Addition      |    1      1  0.0s

Best Practices for Modules

A few guidelines keep modules easy to use and maintain.

Source

Julia Modules documentation

Julia Documentation manual

In this article, we have explored various examples of using modules in Julia, including basic definitions, nested modules, precompilation, and testing. To see how custom types work with generic functions across modules, continue with Julia interfaces; the functions being grouped here are introduced in Julia functions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.