Modules and packages are easy ways to organize code for easy access and easy use.
We can think of modules and packages as the sets or units of code used to simplify tasks and processes that would otherwise be tedious or cumbersome.
Essentially, modules and packages make the coding easier.
In Julia, we use modules and packages in a similar manner.
Functions related to each other and their definitions are stored together in units known as modules in Julia.
The syntax to write our module in Julia takes the below form:
module my_modulename
end
The functions/names/constants etc., can come between the module
and end
.
One or more modules can be stored in a package. Packages in Julia always have .jl
suffix at the end, by convention.
As with most languages, Julia comes with many inbuilt modules and packages for use. It’s advisable to check if the package or module you need is available before writing your own. To use a package/module in Julia, we have to download the package to our local machine and then import it to be available for use.
In Julia’s command line, we need to type ]
the right square bracket.
That opens up the package manager. We can then download and install whichever package we need using the add
keyword.
julia> ]
(v1.0) pkg> add DataFrames
... downloading messages....
(v1.0) pkg>
On Jupyter notebooks, use the below structure:
Import the package manager again and then use add
to install any module/package.
import Pkg
Pkg.add("DataFrames")
To use the module, specify the keyword using
.
For instance:
using DataFrames
We can also specify a set of modules:
using Calculus, DataFrames