# How to get parent module of a submodule

**URL:** <https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364>\
**Category:** General Usage\
**Tags:** module\
**Created:** [February 9, 2023, 5:14pm UTC](https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364 "2023-02-09T17:14:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 9, 2023, 5:14pm UTC](https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364/1 "2023-02-09T17:14:25Z")

</div>

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](https://docs.julialang.org/en/v1/stdlib/Logging/#Logging.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.

---

<div class="post-metadata">

**Author:** ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)\
**Post date:** [February 9, 2023, 5:22pm UTC](https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364/2 "2023-02-09T17:22:57Z")

</div>

You could import the parent module via its relative path:

```julia
module Foo

# ...

module Bar

using ..Foo

# ...

end

end

```

The relevant section in the manual is [here](https://docs.julialang.org/en/v1/manual/modules/#Submodules-and-relative-paths)

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [February 9, 2023, 5:29pm UTC](https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364/3 "2023-02-09T17:29:17Z")

</div>

Are you looking for [parentmodule](https://docs.julialang.org/en/v1/base/base/#Base.parentmodule)?

```julia
julia> module Foo
           module Bar
           end
       end
Main.Foo

julia> parentmodule(Foo.Bar)
Main.Foo

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 9, 2023, 5:31pm UTC](https://discourse.julialang.org/t/how-to-get-parent-module-of-a-submodule/94364/4 "2023-02-09T17:31:55Z")

</div>

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