Is there a way to pass a list of packages to `using` in a similar way as `Pkg.add()`

Is there a way to pass a list of packages to using in a similar way as Pkg.add() ?

This works:

package_names = ["DateFormats", "Statistics"] 
using Pkg 
Pkg.add(package_names)

but I can’t do this
using(package_names)
or this
using.(package_names)

I found somewhere the following code to include in the startup.jl file:

import Pkg
package_names = ["DateFormats", "Statistics"] 
for pkg in package_names
    if Base.find_package(pkg) === nothing
       Pkg.add(pkg)
    end
    @eval using $(Symbol(pkg))
end
2 Likes

I wonder if there any disadvantages to allowing using to accept a vector of package names like Pkg.add()

Yes, you’d have to change the parser. using is a keyword, not a regular function for which you can just add a new method.

Is there a function equivalent for using of is that not possible?

No, but you can easily make one:

_using(x::Union{String, Symbol}) = @eval using $(Symbol(x))
2 Likes

I might be missing something, but why not just

using DateFormats, Statistics

i.e., by separating the packages by commas?

The issue is that it would be nice to be able to pass list of package names to both Pkg.add() and using… separating by commas still requires manual entry. I have scripts that I move between machines so I am always adding and using… The solutions above are great… I would think that this functionality would be good to bad in Base

It feels like you should be instantiating a manifest rather than passing around vectors of package names? (doesn’t solve the using point though just to be clear)

1 Like