I have been trying to get my “local modules” workflow from previous {either include or push loadpath} to using excellent @Roger-luo FromFile
. But the case I was using it get me trouble, specifically it fails when included inside begin ...end
For an MWE : I have following file tmod.jl
module tmod
function hello()
println("hello from tmod")
end
function __init__()
hello()
end
end
If, in REPL, doing
using FromFile
@from "tmod.jl" using tmod
it works, if doing
begin
using FromFile
@from "tmod.jl" using tmod
end
I get
ERROR: LoadError: UndefVarError: @from not defined
in expression starting at REPL[1]:3
Why is that ?
For the record, the two others way of doing it
if !@isdefined tmod; include("tmod.jl"); using .tmod; end
and
pushfirst!(LOAD_PATH,@__DIR__)
using tmod
popfirst!(LOAD_PATH)
when used inside begin/end do not suffer with the same trouble.
So, why is that the @from
does not work when is same block as using FromFile
?
Notes
-
Include solution seems a bit “tormented” for me, and puts the module in the Main namespace while the two other ones put it in the Global namespace IIUC.
-
LOAD_PATH manipulation makes me fears that it is not really “Julia-esque” and may be could fail for later Julia versions - may be I should not worry ? Also, will vs-code referencing work with this way of doing things ? (because of the pop of loadpath makes it loose the path to the module?)
-
This is an aside question but not some kind of XY problem, I was trying to get timing info for my "using"s, by way of TimerOutputs, so put those usings in a begin/end block for that. Then it failed ! I know I can can put the
using FromFile
at the beginning of the file and then it should work, but I would like to understand why when this using and the@from
are in the same block, it does not work.
Thanks for comment about my question, and possibly, also, more generally, about best/better local module workflow)