# Find path of imported module

**URL:** <https://discourse.julialang.org/t/find-path-of-imported-module/2254>\
**Category:** General Usage\
**Created:** [February 23, 2017, 5:55pm UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254 "2017-02-23T17:55:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bastikr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bastikr/32/499_2.png) [@bastikr](https://discourse.julialang.org/u/bastikr)\
**Post date:** [February 23, 2017, 5:55pm UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/1 "2017-02-23T17:55:50Z")

</div>

Is there a simple way to find out from where a module is imported? I.e. something like

```julia
using Foo
path(Foo)

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [February 24, 2017, 6:25am UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/2 "2017-02-24T06:25:34Z")

</div>

```julia
Pkg.dir("Foo")

```

or

```julia
using Foo
Pkg.dir(string(Foo))

```

should do it.

---

<div class="post-metadata">

**Author:** ![bastikr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bastikr/32/499_2.png) [@bastikr](https://discourse.julialang.org/u/bastikr)\
**Post date:** [February 24, 2017, 9:00am UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/3 "2017-02-24T09:00:23Z")

</div>

This works only for packages that are installed by the package manager. In fact the package doesn’t even have to be installed - it just returns the directory + the given name:

```julia
julia> Pkg.dir("abcde123")
"/home/sebastian/.julia/v0.5/abcde123"

```

I also want to handle the case where the module is loaded from somewhere else via LOAD\_PATH.

---

<div class="post-metadata">

**Author:** ![ZacLN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacln/32/2748_2.png) [@ZacLN](https://discourse.julialang.org/u/ZacLN)\
**Post date:** [February 24, 2017, 11:34am UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/4 "2017-02-24T11:34:49Z")

</div>

Assuming the module folder follows the standard structure and also that the module defines or extends at least one function this should work:

```julia
function moduleloc(M::Module)
    for n in names(M, true, true)
        if isdefined(M, n) && getfield(M, n) isa Function
            f = getfield(M, n)
            ms = Iterators.filter(m-> m.module==M, methods(f))
            if !isempty(ms)
                dir = join(split(string(first(ms).file), "/")[1:end-1], "/")
                return dir
            end
        end
    end
    return ""
end

```

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [February 25, 2017, 5:33am UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/5 "2017-02-25T05:33:44Z")

</div>

Based on this, but simpler and more robust, consider

```julia
dirname(dirname(Base.functionloc(Foo.eval, Tuple{Void})[1]))

```

This assumes that the module was declared with `module` rather than `baremodule`, and uses the fact that the `module Foo` line is the one credited for the definition of the `Foo.eval` function (which is defined for all `module`s).

---

<div class="post-metadata">

**Author:** ![bastikr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bastikr/32/499_2.png) [@bastikr](https://discourse.julialang.org/u/bastikr)\
**Post date:** [February 27, 2017, 4:57pm UTC](https://discourse.julialang.org/t/find-path-of-imported-module/2254/6 "2017-02-27T16:57:04Z")

</div>

Thank you both very much for taking the time to even implement these functions for me!
