Skip to content
AgileLogics
7 min read

Packages and Paclets

Prerequisites: Scoping and evaluation control

As your code grows, move it out of notebooks into packages — .wl files with their own namespace (context).

A package file

Save this as Geometry.wl:

BeginPackage["Geometry`"];

circleArea::usage = "circleArea[r] gives the area of a circle of radius r.";
squareArea::usage = "squareArea[s] gives the area of a square with side s.";

Begin["`Private`"];

checkLength[v_] := NumericQ[v] && NonNegative[v]

circleArea[r_?checkLength] := Pi r^2
squareArea[s_?checkLength] := s^2

End[];

EndPackage[];
  • BeginPackage["Geometry"]starts theGeometry`` context. Symbols mentioned before Begin — here, the ones with usage messages — are public.
  • Everything between Begin["Private"] and End[] lives in Geometry`Private` — helper functions like checkLength stay hidden.
  • The backquote ` separates context names, like :: or . in other languages.

Loading it

Get["Geometry.wl"]
circleArea[2]
squareArea[-1]
Context[circleArea]
4 Pi
squareArea[-1]
Geometry`
  • Get["file"] (or << file) loads a file each time you call it.
  • Needs["Geometry"]loads a package only if it isn't loaded yet — the usual choice in code that depends on it. It looks forGeometry.wlon the$Path` list of directories.
  • Invalid input (a negative length) doesn't match, so the call stays unevaluated.

Paclets

A paclet is the Wolfram Language's unit of distribution: a folder with a PacletInfo.wl file describing it, plus packages, documentation, and resources.

(* PacletInfo.wl — illustrative *)
PacletObject[<|
  "Name" -> "Geometry",
  "Version" -> "1.0.0",
  "WolframVersion" -> "13.0+",
  "Extensions" -> {{"Kernel", "Root" -> "Kernel", "Context" -> "Geometry`"}}
|>]

With Geometry.wl in a Kernel subfolder:

  • PacletDirectoryLoad["path/to/Geometry"] uses the paclet in place during development.
  • CreatePacletArchive["path/to/Geometry"] builds a .paclet file.
  • PacletInstall["file.paclet"] installs it; then Needs["Geometry"]` works in any session.

Sharing code

  • The Wolfram Function Repository holds community functions you can use directly with ResourceFunction["Name"].
  • The Wolfram Paclet Repository distributes whole paclets, installed with PacletInstall["Publisher/Name"].
  • Keep .wl files in Git — they're plain text, unlike notebooks, so changes are easy to review with a tool like Code Diff.