# Creating variables from file

**URL:** <https://discourse.julialang.org/t/creating-variables-from-file/76179>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 10, 2022, 3:47pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179 "2022-02-10T15:47:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvaes](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@jvaes](https://discourse.julialang.org/u/jvaes)\
**Post date:** [February 10, 2022, 3:47pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/1 "2022-02-10T15:47:51Z")

</div>

Hi everyone,

I am designing a large model that has a lot of parameters that will be imported from a file.  
I can load the file in a dictionary and I would like to define the variables corresponding to each one of these parameter where the name of the variable would be the key and the assigned value the vale.

For example, this work in the global scope:

```julia
d = Dict("a" => 1, "b" => 2)

for param in d
    @eval $(Symbol(param[1])) = @eval $(param[2])
end

```

This is convenient as I can then do

```julia
x = a + b

```

instead of

```julia
x = d["a"] + d["b"]

```

This is convenient because it makes the code more readable. Unfortunately, I think that this does only work in the global scope. Is there a way to do it inside a function?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [February 10, 2022, 4:10pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/2 "2022-02-10T16:10:25Z")

</div>

Do you need a dictionary? You could directly load the variables from the file:

```julia
function readparams()
    include("params.jl")
    a + b
end

```

with params.jl:

```julia
a = 1
b = 2

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 10, 2022, 4:19pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/3 "2022-02-10T16:19:25Z")

</div>

Dynamically creating variable names is not considered good programming practice (in any language, not just Julia). It will make code \*less \* readable, not more.

If you don’t know the symbol names beforehand, you should use the dictionary. If you do know them, just write

```julia
a = d["a"] 
etc. 

```

explicitly.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 10, 2022, 4:22pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/4 "2022-02-10T16:22:27Z")

</div>

My take on this (if the set of parameters is known beforehand) would be to convert the dict to a structure. Then, with `Parameters`, you can “unpack” the variables needed for specific functions to clean up the code:

```julia
julia> using Parameters

julia> @with_kw struct Params
           a::Int
           b::Int
       end
Params

julia> d = Dict("a" => 1, "b" => 2)
Dict{String, Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> pars = Params(a = d["a"], b = d["b"])
Params
  a: Int64 1
  b: Int64 2

julia> pars.a + pars.b
3

julia> @unpack a, b = pars
Params
  a: Int64 1
  b: Int64 2

julia> a + b
3

```

This package can be useful: [https://github.com/Roger-luo/Configurations.jl](https://github.com/Roger-luo/Configurations.jl)

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [February 10, 2022, 4:25pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/5 "2022-02-10T16:25:52Z")

</div>

Or, similarly,  
[mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros (github.com)](https://github.com/mauro3/Parameters.jl)

---

<div class="post-metadata">

**Author:** ![jvaes](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@jvaes](https://discourse.julialang.org/u/jvaes)\
**Post date:** [February 10, 2022, 4:31pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/7 "2022-02-10T16:31:19Z")

</div>

Great thank for all your responses! It is really helpful 🙂

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 10, 2022, 5:21pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/8 "2022-02-10T17:21:19Z")

</div>

May I ask how do you load a dictionary from a text file?

---

<div class="post-metadata">

**Author:** ![jvaes](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@jvaes](https://discourse.julialang.org/u/jvaes)\
**Post date:** [February 10, 2022, 5:27pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/9 "2022-02-10T17:27:15Z")

</div>

This will depend on your application and which file you are reading from. Here an example if you have an Excel file with one sheet name “DataInfo”

```julia
myDataInfoDF = DataFrame(XLSX.readtable(aExcelFileName, "DataInfo")...)
myParameters = Dict()
for myRow in eachrow(myDataInfoDF)
    ... your code ...
    # example:
    # myParameters[myRow["Name"]] = myRow["Value"]
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 10, 2022, 7:42pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/10 "2022-02-10T19:42:02Z")

</div>

Note that this does work but it’s a bit subtle why it works. In particular, the `include` does _not_ create local variables in the body of `readparams`, rather it creates global variables called `a` and `b` which are then used in the computation of `a + b` at the end of the function. If you evaluate the code, you’ll see that `a` and `b` are globals afterwards:

```julia
julia> function readparams()
           include("params.jl")
           a + b
       end
readparams (generic function with 1 method)

julia> readparams()
3

julia> a
1

julia> b
2

```

In Julia you cannot (intentionally) do `include` or `eval` into a local scope. If you want something like that you can instead generate a function definition based on the code, evaluate that definition and then call it (as many times as you want), like this (in a fresh REPL session so that `a` and `b` aren’t defined):

```julia
julia> @eval function useparams()
           $(Meta.parseall(read("params.jl", String)).args...)
           a + b
       end
useparams (generic function with 1 method)

julia> useparams()
3

julia> a
ERROR: UndefVarError: a not defined

julia> b
ERROR: UndefVarError: b not defined

```

What’s the difference? The performance is quite different for one:

```julia
julia> @time readparams()
  0.000571 seconds (123 allocations: 6.875 KiB)
3

julia> @time useparams()
  0.000001 seconds
3

```

This is because `readparams` opens, parses and evaluates the contents of `params.jl` every time you call it and then evaluates `a + b` based on the new global values of `a` and `b`. This is quite slow. On the other hand `useparams` was generated with specific values of `a` and `b` and then when you call it, that fixed code is evaluated and since `a` and `b` are in the function body all it does is return the constant value `3`:

```julia
julia> @code_llvm useparams()
; @ REPL[5]:1 within `useparams`
define i64 @julia_useparams_274() #0 {
top:
  ret i64 3
}

```

Which do you want? It depends. Which behavior to you need? And do you care about the usage of the values you read from the file being fast or not? If you do want to use them and have it be fast, then you need to take a staged approach where you read the values/code from a file, generate code from that, compile that code and then call it as many times as you need to.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [February 10, 2022, 7:49pm UTC](https://discourse.julialang.org/t/creating-variables-from-file/76179/11 "2022-02-10T19:49:36Z")

</div>

> [@StefanKarpinski](#):
>
> In Julia you cannot (intentionally) do `include` or `eval` into a local scope. If you want something like that you can instead generate a function definition based on the code, evaluate that definition and then call it

Thanks for the facts - on this forum [You’ll Never Walk Alone](https://www.youtube.com/watch?v=OV5_LQArLa0).
