Julia Modules and Package Management
A module is used to organize code into an independent namespace to avoid naming conflicts; a package is a module packaged for distribution, installed through the built-in package manager Pkg.
Defining Modules
Use module ... end to define a module:
Example
module
MyModule
export declares names made public (can be written anywhere in the module)
export
myfunc, PI
const
PI =
3.14159
myfunc
(
x
)
= x
2
Names not exported are private to the module
helper
(
x
)
= x +
1
end
Three Ways to Access Module Members
Example
Method 1: qualified access through the module name (always available)
julia
MyModule.
PI
3.14159
julia
MyModule.
myfunc
(
10
)
20
Method 2: using imports the module name and all exported names
julia
using
.
MyModule
julia
myfunc
(
10
)
Exported names can be used directly
20
julia
helper
(
10
)
Unexported names cannot be used; you need MyModule.helper
ERROR: UndefVarError:
`
helper
`
not defined
in
`
Main
`
Method 3: import imports only the module name itself
julia
import
.
MyModule
julia
MyModule.
myfunc
(
1
)
2
Summary of the differences between using and import:
| Statement | effect | Can extend methods without qualification |
|---|---|---|
using M |
Imports the module name + all exported names | No (you must write M.f) |
import M |
Imports only the module name | No |
import M: f |
Imports only the name f | Yes, write new methods for f directly |
using M: f |
Imports only the name f | No |
Note:In the examples above using .MyModule the dot in it means "a module defined in the current scope". Loading a local module from the REPL or a script requires the dot prefix; loading from an installed package just uses the package name, such as
using Example.
include and Multi-File Modules
In real projects, module code is usually split across several files and assembled with include():
Example
module
MyPkg
include
(
"types.jl"
)
Executes the file contents in the current module
include
(
"functions.jl"
)
end
Standard modules
| module | Description |
|---|---|
Main |
The top-level module; the REPL and scripts run in Main by default |
Base |
The base library, available automatically by default (sum, push!, etc. live here) |
Core |
Language built-in functionality, related to the compiler |
| Standard library modules | Ships with Julia, no installation needed, such as Dates, Test, LinearAlgebra, etc.; use using before use |
The Pkg Package Manager
Julia comes with the Pkg package manager, usable in the REPL's Pkg mode (type ] to enter), or callable in code via using Pkg.
Common Commands in Pkg Mode
| Command | Function |
|---|---|
add 包名 |
Install packages |
rm 包名 |
Uninstall a package |
update 包名 |
Update packages (with no arguments, update all) |
status |
Show which packages are installed in the current environment |
activate 路径 |
Activate (switch) the current environment |
test 包名 |
Run a package's tests |
precompile |
Precompile all dependencies |
Example: Install and Use a Third-Party Package
1. Press ] to enter Pkg mode and install the Example package
(
@v1.10
)
pkg
add Example
2. Press Backspace to return to Julia mode, then load and use it
julia
using
Example
julia
hello
(
"RUNOOB"
)
"Hello, RUNOOB"
The equivalent way in code:
Example
using
Pkg
Pkg.
add
(
"Example"
)
Install packages
Pkg.
status
(
)
View installed packages
Using the Standard Library
The standard library is installed with Julia; just use using, no add needed:
Example
julia
using
LinearAlgebra
julia
I
Identity matrix notation
UniformScaling
{
Bool
}
(
true
)
julia
[
1
2
;
3
4
]
I
2
×
2
Matrix
{
Int64
}
:
1
2
3
4
Environments
Julia packages are installed inEnvironment. Each environment is described by a project directory containing two files:
| file | Purpose |
|---|---|
Project.toml |
The project's list of direct dependencies and compatibility constraints, which should be committed to version control |
Manifest.toml |
A full snapshot of the dependency graph (including exact versions of indirect dependencies), ensuring a reproducible environment |
Creating and Using a Project Environment
Example
1. Create the project directory in the terminal
$
mkdir
myproject
&&
cd
myproject
2. Start Julia and activate the current directory as the environment
$ julia
julia
using
Pkg
julia
Pkg.
activate
(
"."
)
Activating project at
`
~/myproject
`
3. Install dependencies, which generates Project.toml and Manifest.toml
julia
Pkg.
add
(
"JSON"
)
4. From then on, starting Julia in this directory automatically uses this environment
Best practices:Do not put all packages in the default environment (@v1.10); instead create a separate environment for each project. This way different projects can use different versions of the same package without interfering, and by committing Project.toml others can reproduce your environment exactly.
Developing Your Own Package
Pkg can quickly generate a package skeleton:
julia> ] generate MyPkg
Generating project MyPkg:
MyPkg/Project.toml
MyPkg/src/MyPkg.jl
The generated main package file structure is as follows:
The MyPkg/src/MyPkg.jl file
module
MyPkg
export lists the functions provided publicly
export
greet
An example function
greet
(
)
=
"Hello from MyPkg!"
end
After starting Julia in the package directory and activating the environment, you can directly use using on your own package:
julia> ] activate MyPkg
julia> using MyPkg
julia> greet()
"Hello from MyPkg!"
The import form when extending Base functions
When adding new methods to Base functions in your own package, it is recommended to declare them explicitly with import:
Example
module
MyPkg
import
Base: show
Only after import can you add new methods to show
struct Temp
celsius::
Float64
end
Extend Base.show for a custom type
function
Base.
show
(
io::
IO
, t::Temp
)
(
io,
"$(t.celsius)°C"
)
end
end