@__DIR__ versus Pkg.dir

Are there any differences between using @__DIR__ from within the package’s home directory and Pkg.dir? I understand that @__DIR__ gives the path to the folder where the current file is in, and Pkg.dir gives the path to the home directory of the given package, but are there any differences in speed, breakability, robustness, or things one should keep in mind when using one and not the other?

Thanks!

Packages are not necessarily installed in the Pkg.dir folder. Just use @__DIR__ if you can.

4 Likes

I see. Cool, thanks.

1 Like

I’ve followed your advice, and it occurred to me, it’s kind of crazy that packages can be installed in different places. It would be nice if we, as developers, could trust at least that much, that the package is in Pkg.dir
Or to put it in another way: the hassle of dealing with putting a package anywhere else other than in the standard place should fall on the user, if said user decided to deviate from standard, not on the developer. So if there are any hoops to jump through when putting a package somewhere weird and still have a functioning system, then that’s up to the user. The developer should be able to assume that packages are where they should be…
Sorry for the rant.

I disagree. What if I want to install a package system-wide instead of per-user? In that case the package goes in /usr/local/share/julia/site/v0.6 instead of the usual Pkg.dir. This isn’t a weird thing to do, so I would hope it works without jumping through too many hoops as a user. I don’t think it’s that much of a burden to write a package that is agnostic about where it is installed. Is there a particular problem you are having trouble with?

I would also add (from a discussion with Jameson a while ago) that there’s a difference between the code/data (read only) location and user data location. The first should be allowed to be anywhere (that julia can find, i.e. LOAD_PATH) while Pkg.dir would fill in the rule for the latter quite well.

OK, I see. I didn’t realize there are two distinct entities for each package: 1) the system read only data and 2) local per-user data.

Mu specific case is that I have a bunch of nested folders in my package and I need to reference stuff. So to get the current directory, move one up, and then into some other folder, I end up doing this: joinpath(first(splitdir(@__DIR__)), "some_folder"). Seems like it would be nice to have a function for that.
Then again, I could just set const src = first(splitdir(@__DIR__)) in the main module file and work with that.

joinpath(@__DIR__, "..", "some_folder")

1 Like

And this works on Windows as well…?

It should. Also even joinpath(@__DIR__, "../some_folder") should work on Windows.

1 Like

Awesome, thanks a lot!