List of loaded/imported packages in Julia
Asked Answered
M

11

14

How can I get a list of imported/used packages of a Julia session?

Pkg.status() list all installed packages. I'm interested in the ones that that were imported/loaded via using ... or import ...

It seems that whos() contains the relevant information (the names and whether it is a module or not). Can the output of whos() be captured in a variable?

Mongolian answered 29/8, 2014 at 20:0 Comment(0)
S
5
using Lazy
children(m::Module) =
  @>> names(m, true) map(x->m.(x)) filter(x->isa(x, Module) && x ≠ m)

children(Main) will then give you a list of modules currently loaded.


Edit: I used Lazy.jl here for the thrush macro (@>>), but you can rewrite it without easily enough:

children(m::Module) =
  filter(x->isa(x, Module) && x ≠ m, map(x->m.(x), names(m, true)))

Alternatively you could add && x ≠ Lazy to the filter to avoid including it.

Stainless answered 31/8, 2014 at 14:10 Comment(2)
Works like a charm. Is it possible to do without relying on Lazy? Because at the moment it is changing the list of loaded modules that we want to get.Mongolian
Looks good! Removing 'Lazy' manually would not work in our case, since we still want to capture it if it was not loaded by us. Your second solution addresses all of this nicely.Mongolian
V
7

Use names, e.g.

julia> using JuMP

julia> using Gurobi

julia> names(Main)
13-element Array{Symbol,1}:
 :Calculus
 :ans
 :JuMP
 :DualNumbers
 :Graphs
 :DataStructures
 :ReverseDiffSparse
 :MathProgSolverInterface
 :Base
 :MathProgBase
 :Core
 :Gurobi
 :Main
Vanward answered 29/8, 2014 at 20:59 Comment(5)
Okay, this seems like a good start. Is it possible to restrict it just to the packages? At the moment, this also includes the variables in the workspace which could conflict with a package name (imagine a variable assignment Distributions = 1 which than can be confused with a loaded package).Mongolian
I don't think its possible, but someone may know something I don'tVanward
Out curiosity, why do you want to know how to do this?Vanward
I'm interested in having an equivalent of the 'sessionInfo' object in R. The idea is to be able to recreate the software environment for reproducing a finding. Therefore, a list of currently loaded packages and its versions would be essential.Mongolian
Currently, you can also obtain the installed package versions via using Pkg; Pkg.status(). See discourse.julialang.org/t/… for details.Nonmetal
P
7

The proposed answers do not work with Julia 1.0 and hence here is a Julia 1.0 version:

filter((x) -> typeof(eval(x)) <:  Module && x ≠ :Main, names(Main,imported=true))

Or you can avoid doing eval (recommended - see https://discourse.julialang.org/t/list-of-use-d-or-imported-modules-in-a-julia-session/94584):

filter((x) -> typeof(getfield(Main, x)) <:  Module && x ≠ :Main, names(Main,imported=true))
Pinto answered 28/10, 2018 at 10:24 Comment(3)
It works in REPL, but in a script I get get "UndefVarError: Pkg not defined" whSimarouba
I checked - it works as script and there is no Pkg in my code.Pinto
you're right. Maybe it happens in my case because I'm running my tests from anonymous modules to isolate themSimarouba
S
6
 Base.loaded_modules_array()

Gives you any Module that Julia loaded.

Base.loaded_modules::Dict{Base.PkgId,Module}

Is a dict with all loaded modules, from which loaded_modules_array generates its output.

Stratification answered 3/8, 2020 at 21:5 Comment(1)
Can also use this dict to search by string: package_loaded(pkg::String) = any(k -> k.name == pkg, keys(Base.loaded_modules)).Epiglottis
S
5
using Lazy
children(m::Module) =
  @>> names(m, true) map(x->m.(x)) filter(x->isa(x, Module) && x ≠ m)

children(Main) will then give you a list of modules currently loaded.


Edit: I used Lazy.jl here for the thrush macro (@>>), but you can rewrite it without easily enough:

children(m::Module) =
  filter(x->isa(x, Module) && x ≠ m, map(x->m.(x), names(m, true)))

Alternatively you could add && x ≠ Lazy to the filter to avoid including it.

Stainless answered 31/8, 2014 at 14:10 Comment(2)
Works like a charm. Is it possible to do without relying on Lazy? Because at the moment it is changing the list of loaded modules that we want to get.Mongolian
Looks good! Removing 'Lazy' manually would not work in our case, since we still want to capture it if it was not loaded by us. Your second solution addresses all of this nicely.Mongolian
A
4

I very much like Przemyslaw's answer and just want to add a slight modification to it, which replaces the explicit :Main by an optional parameter (as a newbie I am not allowed to enter comments)

loadedModules(m::Module = Main) = filter(x -> eval(x) isa Module && x ≠ Symbol(m), names(m, imported = true))

Anabas answered 16/9, 2019 at 12:26 Comment(3)
This was the most helpful answer for me. But do you also know how the get the versions of the loaded modules?Iodide
You are probably aware of Pkg.status() which lists the package's version of the currently active project. Otherwise Base.find_package() returns the location of the package where it would be loaded from if you would issue an importor usingcommand. If you have changed the project file after loading the package you might find a different version listed than is currently active.Anabas
To not truncate the printing of long lists of loaded modules in the REPL one may add also: loadedModules(m::Module=Main) = filter(x->eval(x) isa Module && x≠Symbol(m), names(m, imported=true)) |> x -> show(stdout, "text/plain", x)Gombroon
K
3

Later versions of Julia include the Dict Base.loaded_modules which contains all modules loaded in the current Julia session

julia> Base.loaded_modules
Dict{Base.PkgId, Module} with 194 entries:
  OpenSSL_jll [458c3c95-2e84-50aa-8efc-19380b2a3a95]                  => OpenSSL_jll
  Xorg_xcb_util_jll [2def613f-5ad1-5310-b15b-b15d46f528f5]            => Xorg_xcb_util_jll
  libass_jll [0ac62f75-1d6f-5e53-bd7c-93b484bb37c0]                   => libass_jll
  Qt5Base_jll [ea2cea3b-5b76-57ae-a6ef-0a8af62496e1]                  => Qt5Base_jll
  Xorg_xcb_util_image_jll [12413925-8142-5f55-bb0e-6d7ca50bb09b]      => Xorg_xcb_util_image_jll
  Libgcrypt_jll [d4300ac3-e22c-5743-9152-c294e39db1e4]                => Libgcrypt_jll
  SnoopPrecompile [66db9d55-30c0-4569-8b51-7e840670fc0c]              => SnoopPrecompile
  DataStructures [864edb3b-99cc-5e75-8d2d-829cb0a9cfe8]               => DataStructures
  Main [top-level]                                                    => Main
  REPL [3fa0cd96-eef1-5676-8a61-b3b8758bbffb]                         => REPL
.
.
.
Kamenskuralski answered 14/2, 2023 at 5:14 Comment(0)
T
2

So this is not as nice of a one-liner, but: It works on v1.0, and it allows for a simple recursive search to show all the loaded modules:

function children(m::Module)
    ns = names(m, imported=true, all=true)
    ms = []
    for n in ns
        if n != nameof(m)
            try
                x = Core.eval(m, n)
                x isa Module && push!(ms, x)
            catch end
        end
    end
    ms
end

function children_deep(m::Module)
  cs = children(m)
  for c in cs
      cs = union(cs, children(c))
  end
  cs
end

Then:

julia> children_deep(Main)
43-element Array{Any,1}:
 Base
 Core
 InteractiveUtils
 Base.BaseDocs
 Base.Broadcast
 Base.Cartesian
 Base.Checked
 Core.Compiler.CoreDocs
 ⋮
 Base.Sys
 Base.Threads
 Base.Unicode
 Base.__toplevel__
 Core.Compiler
 Core.IR
 Core.Intrinsics
 Main
Trough answered 30/4, 2019 at 2:23 Comment(0)
R
1

The answer above no longer works as before in Julia 0.5. It works in many cases, e.g.:

children(SIUnits) -> SIUnits.ShortUnits 

But a majority of packages (that I use) do not actually define submodules . I find this useful for debugging, in Julia command-line version and in one-more-minute's excellent Juno IDE:

loadedmodules() = filter(names(Main, false)) do n 
                                          isa(eval(n), Module) && n ≠ Main
                                        end
Rianna answered 26/12, 2016 at 19:25 Comment(0)
A
1

I use,

using Pkg
function magic()
    println("Julia " * string(VERSION))
    for (key, version) ∈ sort(collect(Pkg.installed()))
        try
            isa(eval(Symbol(key)), Module) && println(key * " " * string(version))
        end
    end
end
Actinomycosis answered 4/4, 2018 at 8:9 Comment(0)
S
0

Looks like names only provides the imported modules from Main, but not from other modules.

    function getLoadedModules(m::Module=Main)
               return filter(x -> Core.eval(m, x) isa Module && x ≠ Symbol(m), names(m; imported=true))
    end
    module QQ
           using Dates
           println(Main.getLoadedModules(QQ))
    end
    println(getLoadedModules(QQ))
    using Dates
    println(getLoadedModules(Main))
    
WARNING: replacing module QQ.
    [:QQ]
    [:QQ]
    [:Base, :Core, :Dates, :InteractiveUtils, :QQ]
Simarouba answered 29/4, 2022 at 21:10 Comment(0)
G
0

This would provide an array of all modules (as strings) that are currently loaded:

all_modules = map( x->String(x), names(Main,imported=true))
Gorse answered 24/5, 2023 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.