# Can not cahnge variables defined in an external file

**URL:** <https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214>\
**Category:** General Usage\
**Tags:** question, module\
**Created:** [May 10, 2020, 7:55am UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214 "2020-05-10T07:55:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [May 10, 2020, 7:55am UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/1 "2020-05-10T07:55:07Z")

</div>

Let us assume that I define some variables in a separate file “input.jl”.  
For example, let the content of “input.jl” be

```julia
a = 123.0

```

Then, I want to use the name of this file as an input parameter to dynamically load the above variables through include:

```julia
module MyModule

function run(fname)
    include(fname)
    
    @show a
    b = a / 2.0
    # a = a / 2.0

    return nothing
end

end

import .MyModule

MyModule.run("input.jl")

```

After running this script I see

```julia
a = 123.0

```

However, If I uncomment the line with “a = a / 2.0”, I obtain the following error message:

```julia
ERROR: LoadError: UndefVarError: a not defined

```

Thus, it seems that I can not change a variable included from external file.  
Can you, please, explain me why it happens.

Thank you.

---

<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:** [May 10, 2020, 1:12pm UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/2 "2020-05-10T13:12:43Z")

</div>

Include runs in global scope and you get the error because the assignment makes `a` local.

There is no function to include a file locally with a runtime filename.

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [May 11, 2020, 9:00am UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/3 "2020-05-11T09:00:12Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [May 11, 2020, 9:48am UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/4 "2020-05-11T09:48:25Z")

</div>

Leaving aside the `include(fname)` situation, this will also happen if you define `a` as a variable inside the module:

```julia
module MyModule
a=123.0
function run(fname)
    a = a / 2.0
    return nothing
end
end

```

Shouldn’t the error message be a bit more informative? Something like: `Cannot reassign "a"(defined in global scope of MyModule) in local scope`.

---

<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:** [May 11, 2020, 1:19pm UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/5 "2020-05-11T13:19:44Z")

</div>

The error message can potetially be improved but wha you give is not the issue. It is perrfectly fine to assign to a global, you just need to define it. Here `a` is a local variable in `run` so it doesn’t have a value before you assigned it.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 11, 2020, 2:10pm UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/6 "2020-05-11T14:10:29Z")

</div>

What yuyichao means is that you need to do:

```julia
module MyModule
a=123.0
function run(fname)
    global a
    a = a / 2.0
    return nothing
end
end

```

If you not do this, then the error message is just exactly the problem, there is no `a` define in local scope. Maybe the code for the error could do something like checking all the global names for the same name (or a very similar one) and suggest using `global` if it is the case. But I do believe it would be expected that a programmer facing this error searched for “julia scoping rules” to understand why `a` is not available at the method scope and discovered the keyword `global`.

Maybe the error message could be changed to `ERROR: LoadError: UndefVarError: a not defined. If a is a global variable referred in local scope please prefix the first mention to the variable in the local scope with the keyword global.`, but most of the time this is probably not the problem, and would only make novice programmers more confused.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [May 11, 2020, 2:22pm UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/7 "2020-05-11T14:22:31Z")

</div>

I am aware of the scope rules and that one needs to use `global`. I just wanted to point out the error message could be a bit more friendly. I actually like your version of error message.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 11, 2020, 2:40pm UTC](https://discourse.julialang.org/t/can-not-cahnge-variables-defined-in-an-external-file/39214/8 "2020-05-11T14:40:45Z")

</div>

Oh, sorry, I thought that I was replying to the person that initially asked the question (@fedoraoff), XD.

Well, you can suggest the error message to the Julia developers, but I really think it becomes kinda long and potentially confusing for newcomers. Probably the most common error is just having a typo in the variable name, and this is not mentioned in the error message. If the error message needs to consider all the most common reasons it will become very large. There was an idea discussed here on Discourse about creating a package that would improve on error messages and could be installed by newcomers as a training wheel while they are learning the language, but initially the idea was to use the dynamic dispatch and define error messages to operators/method calls that people expected to be defined for some type parameters but are not, and suggest the right method/operator for that situation.
