Categories: Julia

Packages in Julia

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.

Pkg

First things first, installation. Well, if you installed Julia, you already have Pkg installed and set-up!

Python has 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:

using foobar

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 " and '

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 pin(comparable to pip freeze):

(@v1.5) pkg> pin foobar

Removing Packages

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")

Bonus: Preview

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).

References

  1. Pkg Documentation
Editorial Staff

Share
Published by
Editorial Staff

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

7 months ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

1 year ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

1 year ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

2 years ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

2 years ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

2 years ago