Developing Julia Packages

Creating a package

Navigate to a directory on your computer where you want to keep your Julia packages under development. It appears that ~/.julia/dev is a pre-determined location that has been set aside for this.

Fire up Julia and enter package mode with ]. Generate the new package with the package command:

(v1.6) pkg> generate NewPackage

Doing this will automatically generate a minimal Julia Package directory structure.

However, you can use the Julia package PkgTemplates to generate a more robust package directory structure, including tests.

julia> using PkgTemplates

julia> Template(interactive=true)("NewPackage")

Follow the prompts, though a git hosting service username is required with PkgTemplates.

Virtual environments

A Julia project environment is simply a directory with a Project.toml file and (optionally) a Manifest.toml file1. To activate an environment, navigate to a directory with the Project.toml file and issue:

(@v1.6) pkg> activate .

You can also name the target environment explicitly:

(@v1.6) pkg> activate /path/containing/project/toml/file

Note that any directory navigation can be done inside the Julia REPL by entering the shell mode via ;.

Package dependencies

Other “third-party” packages that your own package depends on should be listed in the Project.toml file. This is most easily maintained by activating the project environment, then adding dependencies to that environment e.g.,

(@v1.6) pkg> activate NewPackage

(NewPackage) pkg> add StatsBase
...

Adding packages in this fashion means that the [deps] section in the Project.toml file will be automatically updated with each package you add to or remove from the project environment.

Writing tests

There is extensive documentation on writing unit tests, including how to group tests into sets i.e., @testset on the Julia documentation website https://docs.julialang.org/en/v1/stdlib/Test/.

Running tests

It’s worth remembering that you can run package tests inside very easily within the REPL. Activate the project’s environment, then issue:

(NewPackage) pkg> test

This will run any testing blocks in the package’s test/runtests.jl file.


  1. https://docs.julialang.org/en/v1/manual/code-loading/#Environments ↩︎