How can I figure out where the calling function is located? I want an include to be relative to the function itself.
Not sure I understand the “include relative to the function” part, but you can find where the function is located with the help of @which
macro
julia> using Plots
julia> @which plot([1], [1])
plot(args...; kw...) in Plots at /home/skoffer/.julia/packages/Plots/oZheM/src/plot.jl:49
2 Likes
It sounds like you want stacktrace()
to me which returns an Array containing the stack:
julia> example() = stacktrace()
example (generic function with 1 method)
julia> example_1() = example()
example_1 (generic function with 1 method)
julia> example_1()
15-element Array{Base.StackTraces.StackFrame,1}:
example at REPL[7]:1 [inlined]
example_1() at REPL[8]:1
top-level scope at REPL[9]:1
eval(::Module, ::Any) at boot.jl:331
eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:134
repl_backend_loop(::REPL.REPLBackend) at REPL.jl:195
start_repl_backend(::REPL.REPLBackend, ::Any) at REPL.jl:180
run_repl(::REPL.AbstractREPL, ::Any; backend_on_current_task::Bool) at REPL.jl:292
run_repl(::REPL.AbstractREPL, ::Any) at REPL.jl:288
(::Base.var"#807#809"{Bool,Bool,Bool,Bool})(::Module) at client.jl:399
#invokelatest#1 at essentials.jl:710 [inlined]
invokelatest at essentials.jl:709 [inlined]
run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at client.jl:383
exec_options(::Base.JLOptions) at client.jl:313
_start() at client.jl:506
So you want want stacktrace()[2]
as the calling method, I think.
I finally found it, @__FILE__
or @__DIR__
.