# Dynamically constructing a using statement

**URL:** https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585
**Category:** New to Julia
**Created:** [January 25, 2018, 2:17am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585 "2018-01-25T02:17:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [January 25, 2018, 2:17am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/1 "2018-01-25T02:17:16Z")

</div>

I think it should be doable with `@eval` but I’m stuck at the moment… I have a set of packages in an array and I’d like to run a `using` statement on them. My first attempt of constructing a comma-delimited string doesn’t work…

```julia
julia> x = ["BenchmarkTools", "Plots"]
2-element Array{String,1}:
 "BenchmarkTools"
 "Plots"

julia> pkgs = join(x, ",")
"BenchmarkTools,Plots"

julia> @eval using $pkgs
ERROR: TypeError: import or using: expected Symbol, got String
Stacktrace:
 [1] eval(::Module, ::Expr) at ./sysimg.jl:23

```

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [January 25, 2018, 2:55am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/2 "2018-01-25T02:55:23Z")

</div>

x = [:BenchmarkTools, :Plots]  
e = Expr(:toplevel, map(x → Expr(:using, x), x)…)  
eval(e)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 25, 2018, 6:09am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/3 "2018-01-25T06:09:48Z")

</div>

@bramtayl’s answer should be helpful, but can you give a bit more context? _Why_ are you trying to do this? Depending on the objective, perhaps there is a simpler way of achieving it.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [January 25, 2018, 6:23am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/4 "2018-01-25T06:23:37Z")

</div>

I’m trying to force precompilation of all packages so that the package images are written then I can fix the Unix file permissions… I think I submitted another post some time ago regarding the file permission problem. So this is really a workaround.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 25, 2018, 6:39am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/5 "2018-01-25T06:39:10Z")

</div>

I see. You may be interested in these comments:  
[https://github.com/JuliaLang/julia/issues/16409#issuecomment-228873060](https://github.com/JuliaLang/julia/issues/16409#issuecomment-228873060)  
and  
[https://github.com/JuliaLang/julia/issues/16409#issuecomment-242360802](https://github.com/JuliaLang/julia/issues/16409#issuecomment-242360802)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 25, 2018, 7:39am UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/6 "2018-01-25T07:39:51Z")

</div>

Use symbols instead of strings in `x` then just do

```julia
for pkg in x
    @eval using $pkg
end

```

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [January 26, 2018, 10:14pm UTC](https://discourse.julialang.org/t/dynamically-constructing-a-using-statement/8585/7 "2018-01-26T22:14:25Z")

</div>

I am still newbie but would try something like this:

```julia
    for pkg in x
        try
            info("importing: $pkg")
            @eval import $(Symbol(pkg))
            # next line you probably want to change file permission 
            run(Cmd(["ls", "-la", joinpath(replace(Pkg.dir(), ".julia", ".julia/lib"), pkg*".ji")]))
        catch e
            println("$e")
        end
    end

```

import could probably avoid some name clash problems…

run(Cmd(…)) is not the best (replace is ugly hack too) and you’ll very probably find better way. 🙂
