Is it possible to know if module is imported from another one?

As in the topic, I’d like to execute some code (e.g. in __init_) based on the information that my module has not been used/imported into Main. I tried parentmodule and Base.moduleroot etc. but since Main is no longer special in julia, each module is it’s own root…

MWE (works only if module is defined in Main):

julia> module InnerModule
           function __init__()
               parent_mod = parentmodule(@__MODULE__)
               @info "imported from $(last(fullname(parent_mod)))"
           end
       end
[ Info: imported from Main
Main.InnerModule

julia> module OuterModule
           module InnerModule
                  function __init__()
                      parent_mod = parentmodule(@__MODULE__)
                      @info "imported from $(last(fullname(parent_mod)))"
                  end
          end
          import .InnerModule
       end
[ Info: imported from OuterModule
Main.OuterModule
1 Like