liveBook · Manning

Package

As explained in Julia as a Second Language

A package is a distributable unit of software containing reusable code and metadata that can be installed and managed by a package manager.

As a project grows, related functions become hard to manage as loose source files. A Julia package bundles code, resources, and metadata so the code can be reused, distributed, and versioned. Packages can depend on other packages. section 16.0 section 16.1.2

A package is not the same thing as a module. The package is the bundle that Julia locates and manages; its same-named module groups functions and types in a namespace. For example, `using Dates` finds the Dates package and loads its Dates module. section 16.1.2

shell> cd hobby/
~/dev/hobby

(hobby) pkg> generate ToyGeometry
 Generating  project ToyGeometry:
   ToyGeometry/Project.toml
   ToyGeometry/src/ToyGeometry.jl

`generate ToyGeometry` creates a minimal package with `Project.toml` and `src/ToyGeometry.jl`. The package is also its own environment, so dependencies can be added to it rather than to a separate working environment. section 16.2.1

A practical development path is:

  • Activate the package environment before adding its dependencies. `Project.toml` records package identity and direct dependencies; `Manifest.toml` records dependency details, including dependencies of dependencies. section 16.2.1
  • Put related code in separate `src` files and call `include` inside the module definition to make those definitions part of the module. section 16.2.2
  • In a working environment, use `dev` with the package directory while actively changing it. Unlike `add`, this makes local code changes available without an explicit package update. section 16.3

Load and call a local package

After `dev ./ToyGeometry` in the working environment, run `using ToyGeometry` and call `ToyGeometry.sphere_volume(4)`. Loading the package alone does not make every function available by an unqualified name: `sine(π/2)` fails until `sine` is exported, though `ToyGeometry.sine(π/2)` works. section 16.3

Adding a package to an environment makes it available to load; it does not itself load the functions and types into the REPL. Use `using` or `import` when you need them. The active environment also determines which installed version is loaded. section 16.1.1

How Julia as a Second Language covers it

How deep it goes
Works through generation, dependency files, source organization, and local development, while keeping module nesting outside its scope.
Examples
Uses a geometry package with volume and trigonometric functions, then compares its sine function with Julia’s built-in sine.
What it assumes
Builds on Julia REPL use and existing function code, while explaining the package–module distinction and package scaffolding.
What you can do after
Operate/configure it with Julia’s package manager to generate a package, declare dependencies, and develop and load local code.
How it connects
Connects packages to environments, dependency management, modules, namespaces, and exports.

Also explained in

Compare how 8 books explain package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.