Modules, Packages & Environments
Programmatic: using Pkg; Pkg.add("DataFrames").
17.1 Modules
using Pkg — brings exported names into scope.
using Pkg: f, g — only specific names.
import Pkg — only the module name; required to add methods via Pkg.f(...) = ... (or import Pkg: f).
include("file.jl") — paste a file's code into the current module.
module Geometry
export area, Circle # exported names available after `using`
public perimeter # 1.11+: public API without exporting
struct Circle
r::Float64
end
area(c::Circle) = π * c.r^2
perimeter(c::Circle) = 2π * c.r
helper() = "internal"
end # module
using .Geometry # load a local module (note the dot)
area(Circle(1))
Geometry.perimeter(Circle(1))
import .Geometry: helper # bring a specific name (allows extending it)17.2 The package manager (Pkg)
Programmatic: using Pkg; Pkg.add("DataFrames").
pkg> add DataFrames CSV # install
pkg> add [email protected] # specific version
pkg> add https://github.com/user/Pkg.jl
pkg> dev ./MyLocalPkg # develop a local package
pkg> rm CSV
pkg> up # update
pkg> status # (or st) installed packages
pkg> instantiate # install everything in Manifest.toml
pkg> precompile
pkg> gc # remove unused versions
pkg> pin Plots; free Plots
pkg> test MyPkg17.3 Environments
Each project has a Project.toml (direct dependencies & compat bounds) and Manifest.toml (exact resolved versions):
Commit both files for applications (reproducibility); libraries commit only Project.toml. The General registry hosts ~10,000+ registered packages; search at juliahub.com or juliapackages.com.
pkg> activate . # use the environment in the current folder
pkg> activate --temp # throwaway environment
pkg> activate @myshared # shared named environment# Project.toml
name = "MyProject"
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
[compat]
DataFrames = "1.6"
julia = "1.10"17.4 Package extensions & weak dependencies
Since 1.9, packages can define code that loads only when an optional dependency is also loaded ([weakdeps] + [extensions] in Project.toml).