How to get parent module of a submodule

Probably it is something simple and obvious, but I can’t find the solution. Does anyone knows how to get parent module of a submodule? By parent module I mean that if we have submodule Foo.Bar, then it’s parent module is Foo.

Context: I want to filter logs using shouldlog function and I want to exclude logs of some package. shouldlog has _module variable which I want to use for this purpose, but package uses submodules, so I can’t just say _module != Foo I need to extract somehow required information.

Currently I am doing something like !startswith(string(_module), "Foo") but it looks very ugly.

You could import the parent module via its relative path:

module Foo

# ...

module Bar

using ..Foo

# ...

end

end

The relevant section in the manual is here

Are you looking for parentmodule?

julia> module Foo
           module Bar
           end
       end
Main.Foo

julia> parentmodule(Foo.Bar)
Main.Foo
2 Likes

Ah! Thank you! This is exactly what I needed. Do not know why google search results never showed me this function.