How to precompile all modules?

question

#1

Hello.

How can I precompile all modules in Julia from my first session instead of waiting for a while everytime Julia uses a new module?

Is there any fast way to install all Julia available packages or the most common ones without going one per one? Maybe a bunch of common useful packages.


#2

I have this function in my .juliarc:

"Precompile all packages"
function precompile_pkgs end
try
    @eval using ProgressMeter
    @eval function precompile_pkgs()
        @showprogress for pkg in collect(keys(Pkg.installed()))
            if !isdefined(Symbol(pkg))
                info("Importing $(pkg)...")
                try (@eval import $(Symbol(pkg))) catch end
            end
        end
    end
catch
    function precompile_pkgs()
        for pkg in collect(keys(Pkg.installed()))
            if !isdefined(Symbol(pkg))
                info("Importing $(pkg)...")
                try (@eval import $(Symbol(pkg))) catch end
            end
        end
    end
end

which I got from https://groups.google.com/d/msg/julia-users/N2let47lvSY/CNnlWFfhAwAJ
Then run precompile_pkgs() after a Pkg.update().

I think JuliaPro bundles some packages. But anyway, just install the ones you need.


#3

I expected to see a single command :slight_smile:
Thank you very much.


#4

Arguably it should be automatic:


#5

This is so helpful! Thanks


#6

One modification that makes it work here, is that the command doesn’t seemed to hang up on Compat.jl.

So I modified the loop to exclude Compat, as below:

    function precompile_pkgs()
        for pkg in collect(keys(Pkg.installed()))
            if !isdefined(Symbol(pkg)) && pkg != "Compat.jl"
                info("Importing $(pkg)...")
                try (@eval import $(Symbol(pkg))) catch end
            end
        end
    end

Also that bit with ProgressMeter doesn’t work, so at some point I’ll just remove that cleverness. But the function does indeed make everything precompile.