13

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?

Julian
  • 1,271
  • 2
  • 12
  • 17

11 Answers11

8

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
IainDunning
  • 11,546
  • 28
  • 43
  • 1
    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). – Julian Aug 29 '14 at 21:45
  • I don't think its possible, but someone may know something I don't – IainDunning Aug 30 '14 at 01:47
  • Out curiosity, why do you want to know how to do this? – IainDunning Aug 30 '14 at 01:48
  • 1
    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. – Julian Aug 30 '14 at 03:28
  • Currently, you can also obtain the installed package versions via `using Pkg; Pkg.status()`. See https://discourse.julialang.org/t/how-to-see-all-installed-packages-a-few-other-pkg-related-questions/1231/15 for details. – RikH Feb 15 '21 at 20:00
8

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))
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
6
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.

one-more-minute
  • 1,408
  • 10
  • 12
  • 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. – Julian Aug 31 '14 at 19:20
  • 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. – Julian Sep 01 '14 at 04:03
5

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

HHFox
  • 311
  • 2
  • 7
  • This was the most helpful answer for me. But do you also know how the get the versions of the loaded modules? – wueli Jun 30 '20 at 08:07
  • 1
    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 `import`or `using`command. If you have changed the project file after loading the package you might find a different version listed than is currently active. – HHFox Jun 30 '20 at 14:35
  • 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)` – Rafael_Guerra Aug 04 '21 at 07:04
3

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
NHDaly
  • 7,390
  • 4
  • 40
  • 45
3
 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.

Simon Danisch
  • 554
  • 3
  • 8
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
hustf
  • 46
  • 3
1

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
.
.
.
Fredrik Bagge
  • 1,351
  • 8
  • 20
0

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
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]
Daniel Pinyol
  • 2,094
  • 2
  • 15
  • 15
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))
Tintin
  • 1