Appendix B Managing Packages
As with many software packages, additional functionality is available in other libraries or packages. If you take a look at https://juliaobserver.com 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--unless you actually installed it previously. 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 and you can find out more information at the Primes package documentation. This 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:
-
IJulia -- provides the browser interface to Julia. You are probably already using this.
-
ForwardDiff -- does Automatic Differentiation (see Chapter 10)
-
Makie -- A plotting package (See Chapter 14)
-
CairoMakie -- A backend to the Makie package.
-
DataFrames -- a way to nicely handle datasets generally loaded from external files, which are used in Chapter 31
Appendix B will show how to create our own packages also called a module.
Section B.1 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/ 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.11.1 (2024-10-16) _/ |\__'_|_|_|\__'_| | 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.11) pkg>
where the 1.4 will be the version of julia that you are running. There are a number of commands that we will cover here:
and the commands in parentheses are the shortcut.
Subsection B.1.1 Adding a package
In the package command line, type
add \emph{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:
-
You results will vary depending on version numbers avaiable and what subpackages (like
DiffRulesorStaticArrays) are needed for the current version of the package you want to load. -
The line after the 2nd
Updatingline is the package (and version) that you are installing. -
All of the lines after the 3rd
Updatingline is all of the packages that this depends on. -
The + sign means that the package is being added.
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.
Subsection B.1.2 Package Status
Status `~/.julia/environments/v1.11/Project.toml` [336ed68f] CSV v0.10.15 [a93c6f00] DataFrames v1.7.0 [f6369f11] ForwardDiff v0.10.38 [7073ff75] IJulia v1.26.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.
Subsection B.1.3 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:
-
The
rmcommand removes the package from the list of available packages, but doesn’t remove them from your harddrive. -
If you want to see everything installed, navigate to the
\textasciitilde{/.julia/packages} directory, which is where they are stored.
Subsection B.1.4 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.
Subsection B.1.5 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
Subsection B.1.6 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. This is generally not needed anymore and is done automatically.
Subsection B.1.7 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.
