# Get module of a caller

**URL:** <https://discourse.julialang.org/t/get-module-of-a-caller/11445>\
**Category:** General Usage\
**Created:** [June 5, 2018, 1:46pm UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445 "2018-06-05T13:46:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [June 5, 2018, 1:46pm UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445/1 "2018-06-05T13:46:01Z")

</div>

```julia
module A
   foo(???) = println("I've been called from $???")
end

module B
    import A
    A.foo() # should pring "I've been called from B"
end

```

Is it possible to get this behavior, i.e. find out the caller’s module without requiring it to explicitly pass `@ __MODULE__ `? I tired:

```julia
foo(; mod=@ __MODULE__ )

```

But for obvious reasons it returns the module of `foo` itself instead of the caller.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 5, 2018, 2:34pm UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445/2 "2018-06-05T14:34:38Z")

</div>

```julia
julia> module A                       
       function foo()                 
           s = stacktrace()           
           get(s[2].linfo).def.module 
       end                            
       end                            
A                                     
                                      
julia> module B                       
       using A                        
       foo() = A.foo()                
       end                            
B                                     
                                      
julia> B.foo()                        
B                                                                      

```

Obviously needs error handling, probably only works on 0.6, doesn’t quite do what you want (since it needs to be called in a function defined in `B`) and is generally bad since it reaches into internals, but that is at least one way to get what you want 🙂

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [June 6, 2018, 1:16pm UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445/3 "2018-06-06T13:16:26Z")

</div>

Interesting solution, thanks!

For reference, here’s Julia 0.7 version:

```julia
module A                       
function foo()                 
    s = stacktrace()           
    s[2].linfo.def.module 
end                            
end                            

module B                       
using Main.A                        

foo() = A.foo()                

end                            

B.foo() # ==> Main.B

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 9, 2019, 10:41pm UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445/4 "2019-01-09T22:41:20Z")

</div>

This does not seem to work for macros, does somebody know how to access this from a macro?

```nohighlight
module A                       
macro foo()                 
    s = stacktrace()           
    s[end].linfo.def.module 
end                            
end                            

module B                       
using Main.A                        

foo() = A.@foo()                

end                            

B.foo() # WRONG: ==> Main.A

```

it returns `Main.A` instead of `Main.B`, is that a bug?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [January 10, 2019, 9:15am UTC](https://discourse.julialang.org/t/get-module-of-a-caller/11445/5 "2019-01-10T09:15:55Z")

</div>

No, that’s just a consequence of the differences between macros and functions. You want

```julia
julia> module A                       
       macro foo()                 
           __module__
       end                            
       end
Main.A

julia> module B                       
       using Main.A                        

       foo() = A.@foo()                

       end
Main.B

julia> B.foo()
Main.B

```

instead. See [here](https://docs.julialang.org/en/v1/manual/metaprogramming/#Macro-invocation-1) for why that works.
