'List of loaded/imported packages in Julia

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?



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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

Solution 4:[4]

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

Solution 5:[5]

 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.

Solution 6:[6]

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

Solution 7:[7]

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

Solution 8:[8]

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]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IainDunning
Solution 2 Przemyslaw Szufel
Solution 3 HHFox
Solution 4 NHDaly
Solution 5 Simon Danisch
Solution 6 hustf
Solution 7
Solution 8