Initialize a package

I have a package that needs some configuration data. Example data that is needed for tests is included in the package. But when I load the package the data path shall be set to joinpath(pwd(), "data").

Where could I call a function that sets this path correctly when loading the package?

In the moment it is always set to:

julia> get_data_path()

which is correct when testing the package, but wrong when using it…

Using a random package from my default environment:

julia> using JSON3

julia> pkgdir(JSON3, "data")

You can use pkgdir to get the proper directory to point to the data directory. This also works from within the module defining your package.

Other useful tools here could be @__FILE__ and @__DIR__ which expand to the file and directory where the code lives, respectively

1 Like

The only place you can do that is in the __init__() function of the package module.

1 Like

Other options for data dependencies include:

This works:

function __init__()
    set_data_path(joinpath(pwd(), "data"))

Thank you!