# \`eval(Symbol("varname")\` causes \`UndefVarError\` although the variable is defined

**URL:** <https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726>\
**Category:** General Usage\
**Created:** [May 1, 2019, 9:01am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726 "2019-05-01T09:01:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 1, 2019, 9:01am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/1 "2019-05-01T09:01:08Z")

</div>

I am revising the codes of COBRA.jl for using it on julia \> 1.0.  
[https://github.com/opencobra/COBRA.jl/blob/master/test/p\_all.jl](https://github.com/opencobra/COBRA.jl/blob/master/test/p_all.jl)  
[https://github.com/opencobra/COBRA.jl/blob/master/src/distributedFBA.jl](https://github.com/opencobra/COBRA.jl/blob/master/src/distributedFBA.jl) (from Line 857)

During the test of the edited codes, I have got a strange error.  
Here’s a simplified one causing the error.

```julia
function saveDistributedFBA(fileName::String, vars::Array{String,1} = ["minFlux", "rxnsList"], printLevel::Int=1)
  for i = 1:length(vars)
    if isdefined(Main, Symbol(vars[i])) # It is true when i=1
      println("The variable \"minFlux\" actually exists: the type is $(typeof((Main.minFlux)))")
      eval(Main.:minFlux) # This results in UndefVarError
    end
  end
end

```

I performed test by executing  
`(v1.1) pkg>test COBRA`

Is it related to the scope of the variable or a misuse of `eval`?

---

<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:** [May 1, 2019, 9:14am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/2 "2019-05-01T09:14:07Z")

</div>

Most likely that eval is always in global scope.

---

<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:** [May 1, 2019, 9:56am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/3 "2019-05-01T09:56:45Z")

</div>

Although that’s always been the case, so I’m not sure why that would break code from an earlier Julia version.

---

<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:** [May 1, 2019, 10:29am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/4 "2019-05-01T10:29:19Z")

</div>

I can’t repro:

```julia
julia> function saveDistributedFBA(fileName::String, vars::Array{String,1} = ["minFlux", "rxnsList"], printLevel::Int=1)
         for i = 1:length(vars)
           if isdefined(Main, Symbol(vars[i])) # It is true when i=1
             println("The variable \"minFlux\" actually exists: the type is $(typeof((Main.minFlux)))")
             eval(Main.:minFlux) # This results in UndefVarError
           end
         end
       end
saveDistributedFBA (generic function with 3 methods)

julia> minFlux = 2
2

julia> saveDistributedFBA("foo")
The variable "minFlux" actually exists: the type is Int64

```

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 1, 2019, 10:50am UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/5 "2019-05-01T10:50:04Z")

</div>

Yeah, this error is not reproducible when we do just in the REPL as shown in your reply. Actually, this function is in the Module COBRA, resulting conflicts of the scope as Stefan said.

Here is the revised codes for Julia \> 1.0.  
[https://github.com/leejm516/COBRA.jl/tree/master-julia1.0](https://github.com/leejm516/COBRA.jl/tree/master-julia1.0)

---

<div class="post-metadata">

**Author:** ![mcmuffin6o](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mcmuffin6o](https://discourse.julialang.org/u/mcmuffin6o)\
**Post date:** [March 20, 2024, 11:11pm UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/6 "2024-03-20T23:11:28Z")

</div>

Now try that in a script.

---

<div class="post-metadata">

**Author:** ![mcmuffin6o](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mcmuffin6o](https://discourse.julialang.org/u/mcmuffin6o)\
**Post date:** [March 20, 2024, 11:12pm UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/7 "2024-03-20T23:12:32Z")

</div>

Is there any way to do a local scope eval?

---

<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:** [March 20, 2024, 11:13pm UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/8 "2024-03-20T23:13:31Z")

</div>

There is not.

---

<div class="post-metadata">

**Author:** ![mcmuffin6o](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mcmuffin6o](https://discourse.julialang.org/u/mcmuffin6o)\
**Post date:** [March 20, 2024, 11:16pm UTC](https://discourse.julialang.org/t/eval-symbol-varname-causes-undefvarerror-although-the-variable-is-defined/23726/9 "2024-03-20T23:16:20Z")

</div>

Woah, thanks for the prompt reply!

Anyways, those interested in similar functionality might find [this](https://stackoverflow.com/a/74741688) Stackoverflow answer helpful.
