# Met a weird error; UndefVarError

**URL:** <https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312>\
**Category:** New to Julia\
**Tags:** error\
**Created:** [September 9, 2020, 8:36am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312 "2020-09-09T08:36:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 9, 2020, 8:36am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/1 "2020-09-09T08:36:33Z")

</div>

Hi, I’m now writing my codes and encountered a really strange error:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/5/8541e81a57376987bb12393689607ef2f8c6db30.png)  
As can be seen, the error said “`obj_path_tmp` not defined”.  
However, in debugging mode (using Debugger), I can read the value (even add to `obj_linearised`):  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e9024ac0341e7e37d16d1fa6f015da1d5b4eef4.png)

So, what’s wrong with it? It’s really confusing …

EDIT: Is there any function to check if a variable is not defined previously?

P.S. Sorry for the images. I don’t know how to paste the result from REPL.

---

<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:** [September 9, 2020, 9:40am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/2 "2020-09-09T09:40:58Z")

</div>

Please don’t post screenshots (copy should work from most IDEs and the terminal), and make an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<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:** [September 9, 2020, 9:56am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/3 "2020-09-09T09:56:01Z")

</div>

The debugger handles scope slightly wrong when you execute julia code in it, so if you defined `obj_path_tmp` in a more inner scope it is possible the debugger is showing it still being defined while it is not. Here is a MWE:

```julia
1|debug> st
In f() at REPL[11]:1
 1 function f()
 2 for i in 1:5
 3 a = 3
 4 end
>5 print(a)
 6 end

About to run: (print)(Main.a)
1|julia> a
3

1|debug> n
ERROR: UndefVarError: a not defined

```

One of the solutions is to put `local obj_path_tmp` in the same scope as you want to use it:

```julia
julia> function f()
           local a
           for i in 1:5
               a = 3
           end
           print(a)
       end
f (generic function with 1 method)

julia> f()
3

```

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 9, 2020, 11:10am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/4 "2020-09-09T11:10:30Z")

</div>

@kristoffer.carlsson Thank you. Your solution works very well!  
Additional questions:

1. As you answered, adding `local a` works well. However, it looks unnatural to write codes to me. Is there any other elegant solutions?
2. I used to code with Python, and to debug codes, I usually added a short line like  
`import ipdb; ipdb.set_trace()`. Is there any counterpart in Julia like this? In my opinion, using Debugger sometimes bothers me; I have to use a macro `@enter` in front of a function.

I may ask some stupid questions because I’m new to Julia 🙂 Thanks again!

EDIT: your answer is probably related to [here](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope), and it seems in my case that `obj_path_tmp` was declared only in inner `for` loop, and therefore Debugger indicated that `obj_path_tmp` has not been defined “outside the inner `for` loop”. Am I right? (So confusing ☹ )

---

<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:** [September 9, 2020, 11:54am UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/5 "2020-09-09T11:54:28Z")

</div>

> [@iHany](#):
>
> As you answered, adding `local a` works well. However, it looks unnatural to write codes to me. Is there any other elegant solutions?

Well, this is just the way Julia scoping rule works. If you need access to a variable that you declare in an inner scope, outside that scope you need to lift it to the outer scope using e.g. `local`. You could also just declare it like `obj_path_temp = 0.0` or something like that instead of `local` if you are accumulating into it.

> [@iHany](#):
>
> I used to code with Python, and to debug codes, I usually added a short line like  
> `import ipdb; ipdb.set_trace()` . Is there any counterpart in Julia like this?

You can add `@bp` in your code and then run the code with `@run`. The debugger will stop at the `@bp`.

```julia
julia> function g()
           for i in 1:3
               f(i)
           end
       end;

julia> f(i) = (@bp; println("hello $i"));

julia> @run g()
Hit breakpoint:
In f(i) at REPL[22]:1
>1 f(i) = (@bp; println("hello $i"))

About to run: (string)("hello ", 1)
1|debug> c
hello 1
Hit breakpoint:
In f(i) at REPL[22]:1
>1 f(i) = (@bp; println("hello $i"))

About to run: (string)("hello ", 2)
1|debug>
hello 2
Hit breakpoint:
In f(i) at REPL[22]:1
>1 f(i) = (@bp; println("hello $i"))

About to run: (string)("hello ", 3)
1|debug>
hello 3

```

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 9, 2020, 12:06pm UTC](https://discourse.julialang.org/t/met-a-weird-error-undefvarerror/46312/6 "2020-09-09T12:06:27Z")

</div>

Thank you.  
How Julia works is quite different from Python.  
Actually, REPL is also an inconvenient part of Julia to me cuz I used to edit and run codes via vim and terminal. Probably I have to get used to Julia’s grammar and convention ☹  
I really hope that it’s not familiar for a while, and give me both fast code development and high performance.

Anyway, thanks for your kind explanation 🙂
