# Path of this package?

**URL:** https://discourse.julialang.org/t/path-of-this-package/16401
**Category:** General Usage
**Tags:** question
**Created:** [October 16, 2018, 1:33pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401 "2018-10-16T13:33:07Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [October 17, 2018, 5:04pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/21 "2018-10-17T17:04:52Z")

</div>

Unless you are in `Main` the result given by `Base.find_package("Foo")` is very likely to be wrong. Well, it’s currently probably right because nobody is currently relying on the new ability to load different packages with the same name, but in the future it will suddenly break when people do that. You can’t have a function like `Pkg.dir("Foo")` that works correctly, you need to give it the context of where you currently are. To do this correctly, you can call `Base.find_package` with the current module as the first argument:

```julia
julia> Base.find_package(@ __MODULE__ , "BenchmarkTools")
"/Users/stefan/.julia/packages/BenchmarkTools/dtwnm/src/BenchmarkTools.jl"

```

We could expose that as an official API but I’m just not sure this entire approach where people want to know where random packages live is a great idea in the first place. So feel free to use this, but it’s not officially sanctioned.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [October 17, 2018, 5:07pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/22 "2018-10-17T17:07:42Z")

</div>

> [@StefanKarpinski](#):
>
> We could expose that as an official API but I’m just not sure this entire approach where people want to know where random packages live is a great idea in the first place. So feel free to use this, but it’s not officially sanctioned.

Yeah I hear what you are saying, and I think you are probably right that it shouldn’t be exposed as an official API, it’s likely to lead to confusion. Again, most of us here are operating from the context “I only ever want to use the most recent version of a package and I always make sure they have distinct names”. Hopefully even for non-bleeding-edge users that will be true about 95% of the time, but chaos would ensue the remaining 5% of the time.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 17, 2018, 5:08pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/23 "2018-10-17T17:08:56Z")

</div>

I still haven’t seen a compelling use case here so it is a real possibility that this discussion suffers from the xy problem.

A concrete use-case is when you are writing tooling. Then you need to be able to tell what exact files are being loaded etc.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [October 17, 2018, 5:12pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/24 "2018-10-17T17:12:10Z")

</div>

> [@kristoffer.carlsson](#):
>
> A concrete use-case is when you are writing tooling. Then you need to be able to tell what exact files are being loaded etc.

That’s true, but for tooling it’s acceptable to use an internal API that might change in the future.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 17, 2018, 5:32pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/25 "2018-10-17T17:32:34Z")

</div>

My point was just that it is a valid use case to want to know about absolute paths to packages.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [October 17, 2018, 5:39pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/26 "2018-10-17T17:39:48Z")

</div>

I have to do a lot of annoying CI/CD stuff in the next few weeks or months, I’ll let you know if I start seeing many really solid use cases for this. At present I still feel rather ignorant about what CI/CD scripts will look like, I just sort of feel like “Oh shit, I’m going to need that”.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 17, 2018, 5:45pm UTC](https://discourse.julialang.org/t/path-of-this-package/16401/27 "2018-10-17T17:45:18Z")

</div>

Imagine the number of things that are running on CI that does not have a `Pkg` that tells you the pwd 😉

---

<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: [October 18, 2018, 1:08am UTC](https://discourse.julialang.org/t/path-of-this-package/16401/28 "2018-10-18T01:08:43Z")

</div>

This is what I ended up placing into my `startup.jl` to help navigate

```nohighlight
cdpkg(pkg) = cd(dirname(Base.find_package(string(pkg))))

```

This is only meant to make my REPL workflow faster, not for use in packages themselves, only to help navigate and edit them faster, since I use `;vim` from inside the julia REPL, additionally you can define

```nohighlight
macro cdpkg(pkg)
    cdpkg(pkg)
    return nothing
end

```

Then you can navigate like this `@cdpkg ModuleName` (less parenthesis hahahahaha)

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [December 14, 2022, 11:11am UTC](https://discourse.julialang.org/t/path-of-this-package/16401/29 "2022-12-14T11:11:04Z")

</div>

Not sure when `pkgdir()` was first introduced, but as of Julia 1.7, I think a better method to address the OP’s original question is to call `pkgdir(@ __MODULE__ )` inside the `module` of the package being developed. This returns the root directory of the package.

You can further specify the relative path to the root directory as the second argument of `pkgdir()`. For example, `pkgdir(@ __MODULE__ , "src")` returns the `src/` directory of the package.

[Previous page](https://discourse.julialang.org/t/path-of-this-package/16401.md?page=1)
