If I have a module A importing a package B and a module C importing the package B, How can do to import the module B only one time so that module A and module C can use it
Maybe I am not fully getting you question, but if B
is indeed package (added using Pkg) there is no problem in using
multiple times. Do you encounter any problems with this?
module A
using B
# code that usese B
end
module C
using B
# code that usese B
end
If all A
, B
and C
are modules in files it is a bit more complicated, but it would look roughly like this:
module MainWorkingModule
module B
# define stuff
end
module A
using ..B # i.e. use the module B known in the parent scope of A
# code that usese B
end
module C
using ..B
# code that usese B
end
end
naturaly, you can split it up in different files:
module MainWorkingModule
include("B.jl")
include("A.jl")
include("C.jl")
end
Note that MainWorkingModule
could be also just Main
, i.e. the repl context.
OP probably believes that every import loads the module’s code separately. As you demonstrate, repeated imports actually reuse a loaded module.
In fact I have a module A that import SimpleDirectMediaLayer, but there is also another separate module that need the same package. Since importing SimpleDirectMediaLayer takes some time, I feared that importing it many times will cause trouble
Importing a package multiple times only actually loads it once, and subsequent times the already-imported module gets reused.
Thanks