Filepaths in a module

I’m having difficulty finding the best way to manage external data files in a module I’m writing.

Say we have a module Foo that we add to our local packages through Pkg.clone("https://repo.com/Foo.git"). In the repository lies a folder input with a data file data.csv inside it.

What is the best method to load this csv file into a dataframe in my module?

Currently I self-reference the module’s local path using Pkg.dir like so:

module Foo
using CSV, DataFrames

loaded_data = CSV.read(joinpath(Pkg.dir("Foo"), "input", "data.csv"));
end

The call to Pkg.dir("Foo") seems a bit brittle here, but it functions for the moment. Is there a better way to do this though?

Try

joinpath(@__DIR__, "..", "input", "data.csv")
2 Likes

Perfect! The @__DIR__ macro was exactly what I was looking for. Thanks.