How to include non-jl files in a package

Can I include data files (.json, .csv) with a Julia package? For example, could I include a file called data.csv in my package structure containing the following text:

x,y
1,2

and a function in test.jl that calls test.csv?

function test()
   tab = CSV.read("test.csv", DataFrame)
   print(tab[1,"x"]+tab[1,"y"])
end

When I build my package, I can’t use include() to access test.csv

julia> include("test.csv")
ERROR: LoadError: UndefVarError: `x` not defined

Is there another mechanism? In R packages, auxiliary data files are stored in the data or inst folders. How does Julia handle this situation?

I’m assuming you meant include("test.jl")?

You should think of include() as just taking the contents of the file and executing it - it’s only meant for Julia code, not for arbitrary files.

You can, you just put them in the repository that holds your code. The trouble with your test() function is that the path to the file "test csv" is going to be relative to the working directory that you call the function from, not relative to where the function was defined.

There are some macros that can get you the path to the file that the code is defined in… I think it would be something like joinpath(@__DIR__, "test.csv") if the csv is in the same directory as your Julia code, but in general I think you’d be better off passing the path to the file as an argument to the function, the eg

function test(file)
   tab = CSV.read(file, DataFrame)
   print(tab[1,"x"]+tab[1,"y"])
end

test("path/to/test.csv")

Or, if it’s really just ever going to use one file, use something like DataToolkit.jl

1 Like

One option is to use the RelocatableFolders package to do just that. I use it in my own package to load several static files. If you navigate into the constants.jl file you can see where I created a path to the ‘data’ folder in the top level of my project directory. This path will work when ran locally as well when it’s built and distributed.

Here’s a link to the file:

1 Like

I don’t understand all the differences, but in addition to DataToolkit.jl mentioned above, there are also DataSets.jl, DataDeps.jl, and the built-in Artifacts that provide functionality that seems to overlap with what you want.