[ANN] AutoPkg.jl - install and use packages in one go

Motivated by the need to create Google Colab notebooks that handle package installation in a very simple, beginner-friendly way (for didactic scenarios), I’ve registered a little package AutoPkg. It provides a macro @autopkg that makes it easy to add and use packages in one go (Colab example link).

Just have a cell like

import Pkg; Pkg.add("AutoPkg"); using AutoPkg

at the top of the notebook (Colab, Jupyter, VS-Code), then you can just do

@autopkg begin
    using Distributions
    using Makie
    import CairoMakie
end

On Google Colab, @autopkg installs packages directly (since the notebooks run in isolated containers anyway), otherwise it activates a temporary Julia project env. It also always sets $JULIA_PKG_PRESERVE_TIERED_INSTALLED to "true".

In addition to notebooks, I’ve also found @autopkg useful to quickly try out some packages on the REPL. It also, at least partially, addresses Pkg issues 3027.

(@autopkg is not intended for Pluto notebooks, Pluto has built-in package management after all.)

@autopkg can handle more complex block of code as well, like

@autopkg begin
    using SomePackage, SomeOtherPackage
    import YetAnotherPackage

    if some_condition
        using OneMorePackage: some_feature
    end
end
8 Likes

For REPL, is it basically the default behavior + auto-click “o + Enter” at the prompt? Or something different?

1 Like

Basically, though @autopkg will activate a temporary project env no matter if the packages are already in the current env or not. And it will always set $JULIA_PKG_PRESERVE_TIERED_INSTALLED (we could make that configurable, if there’s interest). It’s nice to have in VSCode though - at least for me, the VSCode Julia REPL doesn’t offer automatic package installation.

But yes, while if @autopkg can be convenient at the REPL, it’s certainly not a necesscity. It is mainly intended for notebook, and my main use case has been Colab so far.