# UndefVarError when including a snippet of code

**URL:** <https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 1, 2017, 1:05am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376 "2017-03-01T01:05:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 1, 2017, 1:05am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/1 "2017-03-01T01:05:54Z")

</div>

Imagine that you have many plotting calls that you’re using to debug the code that you are developing. At some point you want to be able to erase the plotting code quickly, commit the changes on git and reinsert the plotting code before development can continue.

I tried to solve this problem with a simple `include` as follows:

## foo.jl

```julia
function foo()
  # do something fancy
  fancyobj = ...

  include("plotting.jl")

  # more fancy operations
  fancyobj = ...

  return fancyobj

```

## plotting.jl

```julia
using PyPlot

# plotting code using variables defined in foo
plot(fancyobj)
xlabel(); ylabel()...
title(...)...

```

So whenever I need to commit my changes, I erase a single line `include("plotting.jl")`, commit and insert it back. The problem is that I am getting this `UndefVarError` for `fancyobj`. I assumed that `include` is equivalent to just pasting the code in place, is that incorrect? If this is the expected behavior, what is a better solution to the problem?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 1, 2017, 1:27am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/2 "2017-03-01T01:27:29Z")

</div>

Are you sure it’s not `plot` that’s undefined? You’re `using` inside of a function. I am not sure that works. Just put `using` at the top scope of your module and it should be good.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 1, 2017, 1:35am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/3 "2017-03-01T01:35:52Z")

</div>

@ChrisRackauckas below is a MWE:

## foo.jl

```julia
function foo()
  a = 2
  include("plotting.jl")
end

foo()

```

## plotting.jl

```julia
b = 2*a

```

> $ julia foo.jl

Gives me:

> ERROR: LoadError: LoadError: UndefVarError: a not defined  
> in include\_from\_node1(::String) at ./loading.jl:488  
> in foo() at /home/juliohm/Desktop/test/foo.jl:3  
> in include\_from\_node1(::String) at ./loading.jl:488  
> in process\_options(::Base.JLOptions) at ./client.jl:262  
> in \_start() at ./client.jl:318  
> while loading /home/juliohm/Desktop/test/plotting.jl, in expression starting on line 1  
> while loading /home/juliohm/Desktop/test/foo.jl, in expression starting on line 6

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 1, 2017, 2:05am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/4 "2017-03-01T02:05:49Z")

</div>

`include` works in global scope. It’s not possible to make it reference local variables.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 1, 2017, 2:08am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/5 "2017-03-01T02:08:50Z")

</div>

> [@yuyichao](#):
>
> include works in global scope. It’s not possible to make it reference local variables.

Ahh yeah, that’s it.

If you want to do compile time copy/paste, use a macro. Here’s a version which works on v0.5 and v0.6.

> [@\`@def\` macro generator broken on master](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/3):
>
> I see, name wasn’t escaped: macro def(name, definition) return quote macro $(esc(name))() esc($(Expr(:quote, definition))) end end end @def test begin a = 2 end # Now @test "pastes" the code `a=2` is good now.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 1, 2017, 2:17am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/6 "2017-03-01T02:17:30Z")

</div>

Is there any other alternative that is simpler? I am trying to avoid writing a macro for every plotting snippet, or I probably don’t understand your proposal.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 1, 2017, 2:19am UTC](https://discourse.julialang.org/t/undefvarerror-when-including-a-snippet-of-code/2376/7 "2017-03-01T02:19:23Z")

</div>

Define this macro somewhere in your code:

```julia
macro def(name, definition)
    return quote
        macro $(esc(name))()
            esc($(Expr(:quote, definition)))
        end
    end
end

```

Then you can easily make macros to copy/paste

```julia
@def MyPlot begin
  plot(fancyobj)
  xlabel(); ylabel()...
  title(...)...
end

```

Then to paste it around

```julia
# some code 
@MyPlot # paste here
# some code

```

Defining a macro for each snippet can’t be worse than making a new file for each one.
