How to execute a bit of Julia code/function from a specific directory

I have a folder (“MyFolder”) that looks something like:
MyFolder:

  • data.ext
  • read_data.jl

next, “read_data.jl” looks ike this:

using DataImporter
data = DataImporter.import("data.ext")

Somewhere else I then run

data = include("path/to/MyFolder/read_data.jl")

The problem is that when I do this, the line data = DataImporter.import("data.ext") is executed from the folder where I initially run the code (and so the path to "data.ext" is wrong).

Is there a way to, in

using DataImporter
data = DataImporter.import("data.ext")

add something to ensure that the line data = DataImporter.import("data.ext") (but idealy nothing else) is executed with the file path in relation to the location of the “read_data.jl” script?

Use @__DIR__:

data = DataImporter.import(joinpath(@__DIR__, "data.ext"))
3 Likes

Thanks a lot :slight_smile: