Introducing Julia/Modules and packages

Modules and packages
Julia code is organized into files, modules, and packages. Files containing Julia code use the  file extension.

Modules
Related functions and other definitions can be grouped together and stored in modules. The structure of a module is like this:

and in between these lines you can put functions, type definitions, constants, and so on.

One or more modules can be stored in a package, and these are managed using the git version control system. Most Julia packages, including the official ones, are stored on GitHub, where each Julia package is, by convention, named with a ".jl" suffix.

Installing modules
To use an official (registered) Julia module on your own machine, you download and install the package containing the module from the main GitHub site. A list of all officially registered packages can be found in the Julia General Registry on GitHub, but it's easier to search for packages using the JuliaHub and Julia Packages sites.

To download and install a package, you can use Julia's package manager, Pkg. Start by typing a right bracket  into the REPL to enter the package manager, then use the   command. You don't have to use quotes or the ".jl" suffix.

julia> ] (v1.0) pkg> add Calculus ... messages (v1.0) pkg>

(If you are not directly connected to the internet, you need to give the name of a proxy before calling the package installer.)

The (v1.0) in the prompt tells you that you're working in the default project, "v1.0", in ~/.julia/environments/. There's a new default project for every release, so you'll probably see v1.7 or v1.8 as the default project during 2022. (A project is a typical - and the most common - way to define a Julia environment, which is a set of packages that together define the way Julia code loads and runs.)

If you want to update the packages you have, use the  command:

(v1.0) pkg> up ... messages (v1.0) pkg>

For more information about Julia's powerful package management system, refer to the extensive documentation.

To leave the package manager mode, press the Backspace/Delete key.

Using modules/packages
After installation, to start using functions and definitions from the package, you tell Julia to make the code available to your current session, with the  or   statements, which accept the names of one or more installed modules:

julia> using Calculus julia>

On success, all the definitions in the Calculus package are available for use. If the definitions inside the Calculus modules were exported by the author(s), you can use them without the module name as prefix (because we used ):

julia> derivative(sin, pi/2) 0.0

If the author(s) don't export the definitions, or if we use  rather than , you can still access them, but you have to type the module name as a prefix:

julia> Calculus.derivative(sin, pi/2) 0.0

but that's unnecessary in this particular example, as we've seen.

So when you write your own modules, the functions that you choose to export can be used without the module name as prefix. Those that you don't export can still be used, but only if they are prefixed with the module name. For example, in the module called, the   was exported. So the prefix is optional:

julia> using MyCoolModule julia> MyCoolModule.mycoolfunction "this is my cool function" julia> mycoolfunction "this is my cool function"

Inside the module, this function was exported, using the  statement:

using and import
is similar to, but differs in a few ways, for example, in how you access the functions inside the module. Here's a module with two functions, one of which is exported:

Use  to import the module:

julia> import MyModule julia> mycoolfunction ERROR: mycoolfunction not defined julia> MyModule.mycoolfunction "this is my cool function"

Notice that  could be accessed only when you use the module prefix. This is because the MyModule module was loaded using import rather than using. Similarly for :

julia> mysecretfunction ERROR: mysecretfunction not defined julia> MyModule.mysecretfunction this is my secret function

You can specify a set of modules:

julia> using Color, Calculus, Cairo

Another important difference is when you want to modify or extend a function from another module. You can't use, you have to   the specific function.

Include
If you want to use code from other files that aren't contained in modules, use the  function. This evaluates the contents of the file in the context of the current module, searching relative to the path of the source file from which it is called. It's like you just pasted the code in. This is useful for building code from a number of smaller files.

How does Julia find a module?
Julia looks for module files in directories defined in the LOAD_PATH variable.

julia> LOAD_PATH 3-element Array{String,1}: "@"       "@v#.#" "@stdlib"

To make it look in other places, add some more using :

julia> push!(LOAD_PATH, "/Users/me/myjuliaprojects") 3-element Array{String,1}: "@"       "@v#.#" "@stdlib" "/Users/me/myjuliaprojects"

And, since you don't want to do this every single time you run Julia, put this line into your startup file , which runs each time you start an interactive Julia session.

Julia looks for files in those directories in the form of a package with the structure:

Or, if not in Package form (see below), it will look for a filename that matches the name of your module:

julia> using MyModule

and this would look in the LOAD_PATH for a file called MyModule.jl and load the module contained in that file.

Packages
To see all the packages you've installed in your current project:

julia> ] (v1.0) pkg> status Status `~/.julia/environments/v1.0/Project.toml` [c7932e45] AstroLib v0.3.0 [9e28174c] BinDeps v0.8.8 [159f3aea] Cairo v0.5.2 [49dc2e85] Calculus v0.4.0 [3da002f7] ColorTypes v0.6.7 [5ae59095] Colors v0.8.2 [861a8166] Combinatorics v0.6.0 [34da2185] Compat v0.68.0 [864edb3b] DataStructures v0.8.3 [5789e2e9] FileIO v0.9.0 [53c48c17] FixedPointNumbers v0.4.6 [28b8d3ca] GR v0.31.0 [a2bd30eb] Graphics v0.3.0 [9fb69e20] Hiccup v0.2.1 [a4bce56a] Iterators v0.3.1 [e5e0dc1b] Juno v0.4.1 ... messages ... (v1.0) pkg>

Structure of a package
Julia uses git for organizing and controlling packages. By convention, all packages are stored in git repositories, with a ".jl" suffix. So the Calculus package is stored in a Git repository called Calculus.jl. This is how the Calculus package is organized in terms of files on disk:

Standard libraries
The Base and Core modules are always available in Julia. There is a set of standard (or stdlib) modules that are installed with Julia but are not all loaded at the start of a Julia session.

If a stdlib module is not already loaded, load them in the usual way, with  or.

Julia version 1.7 includes the following packages: