Chapter 6: Loading Packages

Return to all notes

As with many software packages, additional functionality is available in other libraries or packages. If you take a look at [https://juliaobserver.com](Julia’s Package Listing) there are about 2000 packages (as of Fall 2018). Clicking on any of the package names will give additional information about the package including documentation (hopefully).

If you don’t have a package installed and try to use it, like:

using Primes

You will get an error that it isn’t installed. You can add the package by

using Pkg
Pkg.add("Primes")

and after fetching the package, rerunning

using Primes

should no longer give an error. Then, for example, try

isprime(17)

which returns true because 17 is a prime number. This is a nondeterministic function that determines if a number is true. The documentation for the Primes package is at https://github.com/JuliaMath/Primes.jl and demonstrates 4 commands including isprime and factor which returns all prime factors (and the order).

A few other packages that we have seen or will see:

Chapter 14 will show how to create our own packages also called a module.

Managing Packages in Julia

Although we can use the Pkg package to handle packages, this section will use the terminal REPL to handle any non-standard packages. Additional documentation on this is given in [https://docs.julialang.org/en/latest/stdlib/Pkg/](Julia’s package manager help pages) First, open up a terminal version of julia (generally by opening the application that you downloaded). You will get:

              _
   _      _ _(_)_      |  Documentation: https://docs.julialang.org
  (_)    | (_) (_)     |
  _ _   _| |_  __ _    |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.0 (2018-08-08)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

or similar and then

julia>

which means we’re ready to handle julia commands. If we type ], then the prompt turns into:

(v1.0) pkg>

There are a number of commands that we will cover here:

Adding a package

In the package command line, type add package_name to add the package. For example, to add the ForwardDff package:

add ForwardDiff

and it is not installed, you will get something like:

Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Updating `~/.julia/environments/v1.0/Project.toml`
  [f6369f11] + ForwardDiff v0.9.0
  Updating `~/.julia/environments/v1.0/Manifest.toml`
  [9e28174c] + BinDeps v0.8.10
  [bbf7d656] + CommonSubexpressions v0.2.0
  [163ba53b] + DiffResults v0.0.3
  [b552c78f] + DiffRules v0.0.7
  [f6369f11] + ForwardDiff v0.9.0
  [77ba4419] + NaNMath v0.3.2
  [276daf66] + SpecialFunctions v0.7.0
  [90137ffa] + StaticArrays v0.8.3

A few things to note:

If you want to add multiple packages at the same time, say packages A, B and C, type add A B C. You can also add particular versions of a package (often for testing or to avoid a bug). For example, if you want version 0.3.0 of ForwardDiff type:

add ForwardDiff@0.3.0

You will then get info on the dependencies on that version.

Package Status

The status command (or st) will just list all of the main packages installed. For example,

Status `~/.julia/environments/v1.0/Project.toml`
[c52e3926] Atom v0.7.6
[336ed68f] CSV v0.3.1
[a93c6f00] DataFrames v0.13.1
[f6369f11] ForwardDiff v0.9.0
[7073ff75] IJulia v1.12.0
[e5e0dc1b] Juno v0.5.3
[1a8c2f83] Query v0.10.0

and note that these are just the packages added by the add command, not all of the dependencies. If you want all of the dependencies as well, type st --manifest and I get a huge list of packages.

Removing a Package

You can remove a package by typing remove or rm then the package name. If I want to remove the ForwardDiff package, then

remove ForwardDiff

we get the following:

Updating `~/.julia/environments/v1.0/Project.toml`
 [f6369f11] - ForwardDiff v0.3.0
 Updating `~/.julia/environments/v1.0/Manifest.toml`
 [49dc2e85] - Calculus v0.4.1
 [c5cfe0b6] - DiffBase v0.2.0
 [f6369f11] - ForwardDiff v0.3.0
 [77ba4419] - NaNMath v0.3.2

Note:

Updating packages

If you type update or up you will update all of the installed packages (and dependencies). For example:

Updating `~/.julia/environments/v1.0/Project.toml`
 [7073ff75] ↑ IJulia v1.11.1 ⇒ v1.12.0
 Updating `~/.julia/environments/v1.0/Manifest.toml`
 [7073ff75] ↑ IJulia v1.11.1 ⇒ v1.12.0
 [b85f4697] ↑ SoftGlobalScope v1.0.5 ⇒ v1.0.7
 [5e66a065] ↑ TableShowUtils v0.1.1 ⇒ v0.2.0

and all updates will be with an ↑. If you only want to update a single package, type the name after update.

Building Packages

Generally a package is built after it is installed. Building a package might include running code (or unpacking files) after it is installed. Sometimes if things get wonky, rebuilding is a good thing to do.

build

or if you only want say IJulia built,

build IJulia

Precompiling packages

When a package is used, often it requests to be compiled. For example, when using Primes, then following is shown:

[ Info: Precompiling Primes [27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae]

and basically some code is compiled beforehand, generally to speed up code. You can precompile all code with

precompile

and it may take a while, but you won’t have to wait, when you load the package with the using command.

Testing a Packages

To test a packge, say the ForwardDiff package, then

test ForwardDiff

It list all of the dependencies first, and then runs a number of tests (and we will show how to write tests soon) and timing information. After a while, it finishes sucessfully.