Load packages in a list with the using keyword

Is there a way to load multiple packages stored in an array of strings, using the using keyword?

Something like this, except that it works please:
using [“CSV”, “DataFrames”, “Dates”, “Plots”]

Thank you!

I don’t think this exists, nothing I have seen at least.

You could easily implement something like it, though the only way I could think of now involves eval which is not really considered nice to use. But I know too little of how and why this is to say if it would be bad here. Though if you want it this worked for me when testing at least.

function importall(pkgs) 
    ex = Expr(:using, map(pkg -> Expr(:(.), Symbol(pkg)), pkgs)...)  
    eval(ex)
end 

pkgs = ["LinearAlgebra", "Statistics"]
importall(pkgs)
1 Like

It may also be written as a for loop:

for pkg in pkgs
    @eval using $(Symbol(pkg))
end
1 Like

I’m curious why you want to to that.

What was wrong with the comprehension?

And perhaps better to generalize more:

for pkg in pkgs
    @eval @__MODULE__,using $(Symbol(pkg))
end

which would have it work inside modules as expected.

1 Like

I have tried to use the best practice: if no array is to be returned, use a for loop instead of comprehension

2 Likes