Being a homoiconic language, packages assume a particularly simple form in Julia. Similarly, the toolchain follows suit by being simple to the point of being almost invisible. Most languages have a plethora of tooling dedicated to package management, Julia currently just has
Pkg, but it gets the job done, and makes it easy too.
First things first, installation. Well, if you installed Julia, you already have
Pkg installed and set-up!
pip, which is quite separate from python. Julia has
Pkg, which, although technically distinct from Julia, doesn’t much distinguish itself, you can just as easily leverage it in a script as you can in the REPL.
Being a modern, forward-thinking language, Julia allows you to install packages from inside the REPL without having to do the
sys.path voodoo that Python for example requires. Once inside the Julia REPL, you simple press
] key, and voila, you can now install packages from wherever.
Just need to press a key
When you want to exit, you can either do the usual
<Ctrl> + C, or, just hit backspace!
Bonus Tip: if instead of
], you press
?, you can get into help mode and query documentation.
Installing and importing inside a script
If you’re working with Julia on Jupyter, or just writing a script in general, then you’d add a package as follows:
using Pkg Pkg.add("foobar")
The first statement imports
Pkg itself. The second line then uses it to install the (imaginary) package
foobar. Finally, you actually import the package:
And you can do all the above in the Julia REPL as well (on the other hand, in a typical python scenario, you’d have to exit the session to do a
pip install foobar). Also, note that Julia treats
differently, so if you were to do
'foobar' above, you would run into problems.
Installing and importing in a REPL session
As we mentioned, Julia’s REPL features a dedicated mode for package management accessed via
[. So, to do what we did above, you’d do the following (even though its not necessary, as the above will work perfectly fine):
First hop into
Pkg mode and install foobar
(@v1.5) pkg> add foobar
Now go back to normal mode and import the package
julia> using foobar
This saves you a few keystrokes, but more importantly, keeps your REPL history neat and clean. Now, one might be asking doesn’t Julia automatically import a package that’s installed. Well, the reason is performance, on using your first package, you’ll see a message like
Precompiling Pluto [c3e4b0f8-55cb-11ea-2926-15256bba5781], which tells you that you’re losing a bit of your memory and/or disk space to save your CPU and/or GPU time and energy.
Installing Packages from git
The more observant reader would’ve noticed that the earlier screenshot had a github repo’s ssh link (You would’ve also noted that the terminal is running JuliaPro, but that’s besides the point). And that’s not a mistake, installing a package from a git repo is as easy as putting it’s link in front of
add and you can use either the usual HTTPS or SSH link.
Pkg will handle everything else.
Updating and pinning
You can update packages using
up, in REPL you would:
(@v1.5) pkg> up foobar
Similarly in a script
using Pkg Pkg.up("foobar")
But sometimes, you want to stay at a specific version due to compatibility concerns. This is achieved by
(@v1.5) pkg> pin foobar
Finally, removing packages is done via
rm, in REPL you would do:
(@v1.5) pkg> rm foobar
While in a script, you would leverage Pkg:
using Pkg Pkg.rm("foobar")
Now for the dessert, we have
preview, you can use this command to try any of the above mentioned commands without changing the state of your project. Example usage:
(@v1.5) pkg> preview add foobar (@v1.5) pkg> preview up foobar (@v1.5) pkg> preview rm foobar
As homework, try to figure out how to use
preview in a script.(Hint: look at the other commands).