11 - Developing Julia packages

Understanding modules

The main objective of modules is to provide separate namespaces for global names in Julia.
The basic structure of a module is:
1
module ModuleName
2
export myObjects # functions, structs, and other objects that will be directly available once `using ModuleName` is typed
3
4
[...module code...]
5
6
end
Copied!
Module names are customary starting with a capital letter and the module content is usually not indented. Modules can be entered in the REPL as normal Julia code or in a script that is imported with include("file.jl").
There is no connection between a given file and a given module as in other languages, so the logical structure of a program can be decoupled from its actual division in files. For example, one file could contain multiple modules.
A more common way to use modules is by loading a package that will consists, at least, of a module with the same name of the package.
All modules are children of the module Main, the default module for global objects in Julia, and each module defines its own set of global names.
Note: using and import, when they are followed with either Main.x or .x, look for a module already loaded and bring it and its exported objects into scope (for import only those explicitly specified). Otherwise, they do a completely different job: they expects a package, and the package system lookups the correct version of the module x embedded inside package x, it loads it, and it bring it and its exported objects into scope (again, for import x only those explicitly specified).
Modules can include submodules, although there exists a large opinion in the Julia community that this, unless it is really necessary, should be avoided, and use the dot notation to indicate the hierarchy.
The following example uses the Reexport package that provides an handy method to re-export automatically all the objects of an imported module.
1
module Foo
2
export plusOne, Foo3, Foo4
3
plusOne(x) = x + 1
4
plusTen(x) = x + 10
5
module Foo3
6
export plusThree
7
plusThree(x) = x+3
8
end
9
module Foo4
10
using Reexport
11
export plusFour
12
@reexport using ..Foo3 # note the two dots in front to go one level up, as these two modules are siblings
13
plusFour(x) = x+4
14
end
15
end # end Foo
Copied!
Assuming we have loaded it into memory (e.g. by typing it directly in the REPL) we can now use the module in several ways:
1
Foo.plusOne(2) # Ok
2
Foo.plusTen(2) # Ok
3
Foo.Foo4.plusThree(2) # OK
4
plusOne(2) # Error, the objects of Foo are not into scope
5
using .Foo # We bring the exported objects into scope
6
plusOne(2) # Now it works
7
plusTen(2) # Error, plusTen is not exported
8
import .Foo: plusTen # We explicitly bring it into scope
9
plusTen(2) # Now it is in scope
10
plusThree(2) # Error, not in scope
11
Foo3.plusThree(2) # Ok
12
Foo4.plusThree(2) # Ok, as Foo4 re-exports the exported objects of Foo3
Copied!
You can read more about modules in the modules section of the official documentation.

Developing Julia packages

A package is nothing else that a collection of one or more hierarchically connected modules (at least one with the same name of the package acting on top of the hierarchy) logically connected to provide a given functionality, enriched with metadata that make the discovery, usage and interconnection of modules much simpler. Hence, before developing a package, be sure you fully understand how modules behave in Julia, as described in the previous section.
Note: As in the rest of the tutorial, when you read julia> command the command has to be issued in a Julia session, while with (@v1.X) pkg> command the command has to be issued in the special "package" mode of Julia (type ] in a Julia session to access it,[DEL] to return to the Julia main prompt).
We assume that we want to create a package using GitHub as repository host and continuous integration tools. We also assume we are using Julia >= v.1.2.0. We will go trough the process of creating the package from scratch and implement automatic testing (next section), document our newly created package (folowing section) and finally register it within the official Julia Register, so that other people can find and install the package easily (last section).

Creating the package and implementing tests

First, we create the repository in GitHub, considering a package name that follows the Julia's package naming guidelines and, by custom, calling the repository by the package name followed by the ".jl" prefix. For example, in this tutorial we chose as package name MyAwesomePackage, and the corresponding GitHub repository will be MyAwesomePackage.jl.
Don't forget to add a readme (so we can already clone the repository) and choose Julia as gitignore template:
Tip: Are you in a rush? Clone MyAwesomePackage.jl and adapt it to your needs.
The repository doesn't yet contain the minimum set of information that allows it to be downloaded as a "Julia package", hence we will first generate a basic package structure locally, using the Julia tool generate. We will then commit and link this git repository with the remote GitHub repository we just made, push the basic package structure to GitHub and, at that point, we can download it again as a Julia Package and continue its development.
So, let's generate our package locally. We cd to a directory where we want the new package to appear as a subfolder (for example our Desktop), enter the Julia prompt and type (@v1.X) pkg> generate MyAwesomePackage.
This will create a new folder MyAwesomePackage with a src subfolder that includes a "Hello world" version of our awesome package and, most importantly, the Project.toml file. For now, this includes just: (a) the name of the package; (b) the author; (c ) the unique id that has been assigned to the new package (this is a code that depends from stuff like the MAC address, the exact time, the process id, etc...) and (d) the initial version of the package. Check that the author and initial version are ok for you (for example I prefer to start a package with version 0.0.1 rather than the default 0.1.0).
Project.toml will also hold the dependencies that our package will require. So, for example, let's assume that MyAwesomePackage depends from the packages LinearAlgebra (a standard lib package) and DataFrames(a popular third-party package to work with tabular data, aka "dataframes").
We first "activate" the new MyAwesomePackage folder with (@v1.X) pkg> activate(FULL_PATH/MyAwesomePackage). We can now add the two packages with (MyAwesomePackage) pkg> add LinearAlgebra DataFrames. From the message in output, we take note that the DataFrame version is v0.22.2. Going back to our new Project.toml file, the two packages should have been added to the [deps] section. We now want to specify the version of the third-party packages our MyAwesomePackage works with, and in particular, if we want to register it in the official Julia registry, we need to specify an upper version. We can do that by adding to Project.toml a new [compat] section where we write down DataFrames = "0.21, 0.22", that is we allow MyAwesomePackage to work with any DataFrames in the v0.21.x or v0.22.x series. In other words, we assume that the functionality our package depends from has been introduced in DataFrames v0.21.O, and it is still available in the v0.22.x serie.
We also add julia = "1.2.1" assuming that our package works only with Julia >= v1.2.1 within the v1.x serie.
NOTE: All Julia versions in the v1.x serie are "guaranteed" to be backward compatible with code wrote for Julia v1.0.0, but the reverse is not true, e.g. new features could still be added in the v1.x serie so that certain packages work only with them.
On one side this way to manage dependencies brings a considerable burden on the package maintainer, as it is his responsibility to determine which exact versions of the dependent package his package works with, but on the other side it guarantees a smooth usage of his/her package. More information on Julia semantic rules can be found here and in particular the requirements for automatic merging of the package in the registry are given here.
Project.toml should look at the end similar to this :
1
name = "MyAwesomePackage"
2
uuid = "19daf8e9-0870-4f25-9c1d-f38ed379fe1e"
3
authors = ["Antonello Lobianco <[email protected]>"]
4
version = "0.0.1"
5
6
[deps]
7
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
8
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9
10
[compat]
11
DataFrames = "0.21, 0.22"
12
julia = "1.2.1"
Copied!
Our new package is now ready to be linked with the repository we created in GitHub. While in this tutorial we will use a terminal, you can just use the github web interface to copy the files generate has produced to your github repository.
Let leave Julia for now and go to the local repository with a terminal and run the following commands in the terminal:
1
git init # Initialise the git repository
2
git add . # Add all files, including in subfolders
3
git commit -a -m "Initial package structure of MyAwasomePackage" # Create a first commit
4
git branch -m main # Rename "master" to "main" as of the new GitHub policy
5
git remote add origin https://github.com/sylvaticus/MyAwesomePackage.jl.git # Link the remote github repository to the local one
6
git pull origin main --allow-unrelated-histories # Fetch the Readme and gitignore we created when we created the repository
7
git push --set-upstream origin main # Finally upload everything back to the GitHub repository
Copied!
Once we have the package in GitHub, we can add it to our Julia local installation with (@v1.X) pkg> add https://github.com/sylvaticus/MyAwesomePackage.jl.git and (@v1.X) pkg> dev MyAwesomePackage. The package should now located in [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage, where [USER_HOME_FOLDER] is the home folder for the local user, e.g. ~ in Linux or %HOMEPATH% in Windows and we can cancel the original location where we generated it (for example in our Desktop).
Before we add a test suite, let's add a minimum of functionality to our package. I highly advise you to use the Revise package. When you import Revise in a new Julia session before importing a package it lets you account for changes that you make when you save on disk any modification you make on the package without having to restart the Julia session.
So let's add a function plusTwo to our package by editing MyAwesomePackage/src/MyAwesomePackage.jl as follow:
1
module MyAwesomePackage
2
3
export plusTwo
4
5
plusTwo(x) = return x+2
6
7
end # end module
Copied!
We can now add a test suite to our package. We add the following file (and folder) [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/test/runtests.jl:
1
using Test, MyAwesomePackage # This load both the test suite and our MyAwesomePackage
2
3
out = plusTwo(3)
4
5
@test out == 5 # This is the actual test condition. You can add as many tests as you wish.
Copied!
Before we can run the test we still need to tell Julia that when testing the package, the standard lib Test needs to be added to the package:
1
(@v1.x) pkg> activate [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/test/
2
add Test
3
(test) pkg> activate # Without arguments, "activate" brings back to the default environment
4
(@v1.x) pkg> test MyAwesomePackage # This perform the test
Copied!
From now on, to add a dependency:
  • to the package itself --> activate [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage and (MyAwesomePackage) pkg> add MyDependencyPackage --> the new package will be recorded in the [deps] section of [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/Project.toml (don't forget to add a upper bound if the package is not in the standard library)
  • to the testing system only (i.e. something the package doesn't rely to, but it is needed for testing, for example a package to load a specific datasets) --> activate [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/test and (test) pkg> add MyDependencyPackage --> the new package will be recorded in the [deps] section of [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/test/Project.toml
Before we git commit and git push our MyAwesomePackage, let's add a functionality that at each time the package is pushed, github performs an operation (github calls these operation "actions"), and in particular it automatically runs the test for us.
Let's add the folder/file [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/.github/workflows/ci.yml:
1
name: CI
2
on:
3
pull_request:
4
branches:
5
- main
6
push:
7
branches:
8
- main
9
tags: '*'
10
jobs:
11
test:
12
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
13
runs-on: ${{ matrix.os }}
14
strategy:
15
fail-fast: false
16
matrix:
17
version:
18
- '1.3.1' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
19
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
20
os:
21
- ubuntu-latest
22
- macOS-latest
23
- windows-latest
24
arch:
25
- x64
26
steps:
27
- uses: actions/[email protected]
28
- uses: julia-actions/[email protected]
29
with:
30
version: ${{ matrix.version }}
31
arch: ${{ matrix.arch }}
32
- uses: actions/[email protected]
33
env:
34
cache-name: cache-artifacts
35
with:
36
path: ~/.julia/artifacts
37
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
38
restore-keys: |
39
${{ runner.os }}-test-${{ env.cache-name }}-
40
${{ runner.os }}-test-
41
${{ runner.os }}-
42
- uses: julia-actions/[email protected]
43
- uses: julia-actions/[email protected]
44
- uses: julia-actions/[email protected]
45
- uses: codecov/[email protected]
46
with:
47
file: lcov.info
48
docs:
49
name: Documentation
50
runs-on: ubuntu-latest
51
steps:
52
- uses: actions/[email protected]
53
- uses: julia-actions/[email protected]
54
with:
55
version: '1'
56
- run: |
57
julia --project=docs -e '
58
using Pkg
59
Pkg.develop(PackageSpec(path=pwd()))
60
Pkg.instantiate()'
61
- run: |
62
julia --project=docs -e '
63
using Documenter: doctest
64
using MyAwesomePackage # change MyAwesomePackage to the name of your package
65
doctest(MyAwesomePackage)' # change MyAwesomePackage to the name of your package
66
- run: julia --project=docs docs/make.jl
67
env:
68
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
70
- uses: julia-actions/[email protected]
71
- uses: codecov/[email protected]
Copied!
The above script, on top of running the tests, will (1) initialise the service codecov to highlight the lines of code that are actually covered by the testing (you will need to first authorise codecov with the "Add new repository" on its web interface - after you log in), and (2) will start the building of the package's documentation (see the following section). If you need to add some tests that are "allowed" to fail (typically, to test on Julia night builds), you can write a similar workflow file (see an example).
At this point we can add the first "badges" on our package README.md:
1
[![Build status (Github Actions)](https://github.com/sylvaticus/MyAwesomePackage.jl/workflows/CI/badge.svg)](https://github.com/sylvaticus/MyAwesomePackage.jl/actions)
2
[![codecov.io](http://codecov.io/github/sylvaticus/MyAwesomePackage.jl/coverage.svg?branch=main)](http://codecov.io/github/sylvaticus/MyAwesomePackage.jl?branch=main)
Copied!
The first one concerns the building status and the second one the line coverage.
Having an upper limit of the dependencies means that our package, if installed, would "limit" that dependencies in the user's environment. We hence need to continuously check for new versions of the dependencies or it will ends that our package constrains the users to keep old packages installed on his/her system. Something that comes in handy is the following action: it checks for us if any of our dependencies has a new version and open for us a pull request to account for such version in our project's Project.toml. But attention, it is still up to us to check that indeed the new version works with our package!
[USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/.github/workflows/CompatHelper.yml:
1
name: CompatHelper
2
3
on:
4
schedule:
5
- cron: '00 00 * * *'
6
7
jobs:
8
CompatHelper:
9
runs-on: ubuntu-latest
10
steps:
11
- name: Pkg.add("CompatHelper")
12
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
13
- name: CompatHelper.main()
14
env:
15
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
17
run: julia -e 'using CompatHelper; CompatHelper.main()'
Copied!
Also, please note that this action is automatically triggered at any given interval. GitHub stops this kinds of cron-based actions for inactive repositories, sending a warning email to you to prevent it. So, until a more solid solution is found, check your email if your repository is no longer actively developed!
With out package in [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/ we can continue to develop it, commit the changes and push them to GitHub (behind the scenes, the add and dev Julia package commands did actually cloned the GitHub repository to .julia/dev/MyAwesomePackage/, so we can use directly git commands on that directory, like git commit -a -m "Commit message" or git push).
This conclude the section about developing (and testing) a package. You can read more details here, including how to specify a personalised building workflow.

Documenting our package

It is a good practice to document your own functions. You can use triple quoted strings (""") just before the function to document and use Markdown syntax on it. The Julia documentation recommends that you insert a simplified version of the function, together with an Arguments and an Examples sections. For example, this is the documentation string for our plusTwo function.
1
"""
2
plusTwo(x)
3
4
Sum the numeric "2" to whatever it receives as input
5
6
A more detailed explanation can go here, although I guess it is not needed in this case
7
8
# Arguments
9
* `x`: The amount to which we want to add 2
10
11
# Notes
12
* Notes can go here
13
14
# Examples
15
```julia
16
julia> five = plusTwo(3)
17
5
18
```
19
"""
Copied!
The documentation string for a function or a module should be placed exactly on top of the item to document (no empty lines) and it will be rendered when a user type ?<FUNCTION OR MODULE NAME> or in the documentation pages that we are going to build.
In order to process to the building of the documentation pages, we produce a skeleton of the documentation configuration files using the following commands:
1
(@v1.x) pkg> activate # For this step, Documenter and DocumenterTools need to be added to your general Julia registry, not your package specific one
2
(@v1.x) pkg> add Documenter
3
(@v1.x) pkg> add DocumenterTools
4
julia> using DocumenterTools
5
julia> cd("[USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/")
6
julia> DocumenterTools.generate()
Copied!
This should produce an output similar to the following:
1
[ Info: name of package automatically determined to be `MyAwesomePackage`.
2
[ Info: deploying documentation to `~/.julia/dev/MyAwesomePackage/docs`
3
[ Info: Generating .gitignore at /home/lobianco/.julia/dev/MyAwesomePackage/docs/.gitignore
4
[ Info: Generating make.jl at /home/lobianco/.julia/dev/MyAwesomePackage/docs/make.jl
5
[ Info: Generating Project.toml at /home/lobianco/.julia/dev/MyAwesomePackage/docs/Project.toml
6
[ Info: Generating src/index.md at /home/lobianco/.julia/dev/MyAwesomePackage/docs/src/index.md
Copied!
While generate() produce a basic documentation configuration we are going to extend it a bit in order to have (a) multiple pages, (b) automatic inclusion of all documented items and (c ) hosting (deployment) of the documentation in GitHub pages. All these steps are documented in deep in the Documenter own documentation. Note that the docs folder has its own Project.toml, so you can add documentation-specific packages there.
Let's then change the [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/docs/make.jl:
1
using Documenter
2
using MyAwesomePackage
3
4
push!(LOAD_PATH,"../src/")
5
makedocs(sitename="MyAwesomePackage.jl Documentation",
6
pages = [
7
"Index" => "index.md",
8
"An other page" => "anotherPage.md",
9
],
10
format = Documenter.HTML(prettyurls = false)
11
)
12
# Documenter can also automatically deploy documentation to gh-pages.
13
# See "Hosting Documentation" and deploydocs() in the Documenter manual
14
# for more information.
15
deploydocs(
16
repo = "github.com/sylvaticus/MyAwesomePackage.jl.git",
17
devbranch = "main"
18
)
Copied!
Let's consider [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/docs/anotherPage.md:
1
# The MyAwesomePackage Module
2
3
```@docs
4
MyAwesomePackage
5
```
6
7
## Module Index
8
9
```@index
10
Modules = [MyAwesomePackage]
11
Order = [:constant, :type, :function, :macro]
12
```
13
## Detailed API
14
15
```@autodocs
16
Modules = [MyAwesomePackage]
17
Order = [:constant, :type, :function, :macro]
18
```
Copied!
In anotherPage.md we first document the module, we then create an "index" of all the documented items, and we finally detail them.
I also suggests to edit the index.md to provide a link back to the github repository.
We can check that the documentation compiles as desired by running the make.jl as a script from within the docs folder:
1
~/.julia/dev/MyAwesomePackage/docs$ julia make.jl
2
[ Info: SetupBuildDirectory: setting up build directory.
3
[ Info: Doctest: running doctests.
4
[ Info: ExpandTemplates: expanding markdown templates.
5
[ Info: CrossReferences: building cross-references.
6
[ Info: CheckDocument: running document checks.
7
[ Info: Populate: populating indices.
8
[ Info: RenderDocument: rendering document.
9
[ Info: HTMLWriter: rendering HTML pages.
10
┌ Warning: Documenter could not auto-detect the building environment Skipping deployment.
11
└ @ Documenter ~/.julia/packages/Documenter/FuXcO/src/deployconfig.jl:75
12
[email protected]:~/.julia/dev/MyAwesomePackage/docs$
Copied!
This should have produced a static build of the documentation on [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/docs/build. We finally "solve" the warning above and take care of documentation's deployment, whose steps are documented in detail here.
1
(@v1.x) pkg> activate
2
julia> using DocumenterTools
3
julia> using MyAwesomePackage
4
julia> DocumenterTools.genkeys(user="YourUsername",
5
repo="[email protected]:YourUsername/MyAwesomePackage.jl.git")
Copied!
Now copy the first key (the public part starting with "ssh-rsa") to https://github.com/YOUR_USERNAME/MyAwesomePackage.jl/settings/keys -> "Add deploy key" using "documenter" as title of the key and allowing write access to the key.
Finally (almost) in https://github.com/YOUR_USERNAME/MyAwesomePackage.jl/settings/secrets/actions add a new repository secret with "DOCUMENTER_KEY" as secret name and the second, long key from the output of the command above as its value.
We can add the two badges for the stable and development versions of the documentation to the readme:
1
[![](https://img.shields.io/badge/docs-stable-blue.svg)](https://YOUR_USERNAME.github.io/MyAwesomePackage.jl/stable)
2
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://YOUR_USERNAME.github.io/MyAwesomePackage.jl/dev)
Copied!
When we push our commits to GitHub, the CI.yml action that we set earlier (a) build the documentation by running "make.jl" and (b) deploy it to the github pages. The documentation is built on its own branch, gh-pages. This branch is added automatically during the process of deploying the documentation the first time. At the same time the "source branch" for the documentation pages in the repository settings (https://github.com/YOUR_USERNAME/MyAwesomePackage.jl/settings) should already be set to gh-pages (see the GitHub pages documentation to set it manually if this for any reason doesn't happen).
For now, the documentation is available on https://YOUR_USERNAME.github.io/MyAwesomePackage.jl/dev, in the branch gh-pages. The first time we will make a release, its documentation will be available under the stable directory.

Registration of our package

We can finally register our Package, but before let's add a further action to it, the TagBot Action [USER_HOME_FOLDER]/.julia/dev/MyAwesomePackage/.github/workflows/TagBot.yml, documented here:
1
name: TagBot
2
on:
3
issue_comment:
4
types:
5
- created
6
workflow_dispatch:
7
jobs:
8
TagBot:
9
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
10
runs-on: ubuntu-latest
11
steps:
12
- uses: JuliaRegistries/[email protected]
13
with:
14
token: ${{ secrets.GITHUB_TOKEN }}
15
ssh: ${{ secrets.DOCUMENTER_KEY }}
Copied!
When we register a new release in the Julia registry, TagBot will create a corresponding release on our package's GitHub repository.
We are now ready to register the package, with the simplest way being to use the web app "Registrator".
Simply click "Install app" and authorise it to the GitHub repositories you wish (or on all your repositories if you prefer).
Now we can register the package with a simple @JuliaRegistrator register "command" in the comment section of the commit we want to register.
We can also write detailed release notes by adding a Release notes: after the @JuliaRegistrator command, for example:
Congratulation ! A "robot" will respond to your command confirming the opening of the pull request in the Julia registry and, if the package meet all the requirements, it should soon automatically registered (once a 3 days waiting period has elapsed) and everyone will be able to simply add and use it with (@v1.X) pkg> add MyAwesomePackage. If something goes wrong - in our case let's say that we forgot to put the upper bound for Julia itself - you will receive an informative message explaining what went wrong and we can try the registration again (issuing an other @JuliaRegistrator register "command", that in turn will update the pull request).

Further development of the package

In the most basic situation, we would keep the main branch locally, continue our development, and when we consider it is "time" to release a new version we just change the version information in the package Project.toml, we commit/push the new "version" and we raise the @JuliaRegistrator register "command" again in GitHub. This will have the effect to register the new version in the Julia official Register, with TagBot creating an equivalent version on the GitHub repository.
Last modified 2d ago