What is the correct way to import local modules in Pluto v0.18.0 notebooks with Julia 1.7.2?
The using
keyword adds exported members to the main namespace in Julia. Given a sample module, in foo.jl
,
module Foo
export bar
function bar()
return "baz"
end
end
The following code cell works in a Pluto Notebook:
# "baz"
begin
include("./foo.jl")
using .Foo
bar()
end
However, if I attempt to call bar
from another cell, I get the following error:
# UndefVarError: bar not defined
bar()
Though I notice that Foo.bar()
does work.
How do I add my own modules in a way that I get access to their exported members directly in the notebook's namespace?
This discussion gives a possible solution. It describes a better way of getting the module reference than what I have in my question.
If the module is not the last definition in the file, the
import
function has to be redefined. A suggested name is to have a variant called "ingredients
"Then the module can be loaded in like so (I don't know the reason why I have to say
ingredients().Foo
)and then with the module reference, you have to manually bring all exported variables into the global scope: