# Variables not recalculating while reloading modules

**URL:** <https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396>\
**Category:** New to Julia\
**Created:** [September 10, 2020, 4:09pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396 "2020-09-10T16:09:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeep\_prakash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandeep_prakash/32/17620_2.png) [@sandeep\_prakash](https://discourse.julialang.org/u/sandeep_prakash)\
**Post date:** [September 10, 2020, 4:09pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/1 "2020-09-10T16:09:23Z")

</div>

I have a module that i use to input some parameters (used in multiple functions etc, and I did not want to rewrite everywhere). They are lightly manipulated (basic scaling etc) and exported to various parts of a larger code.  
Some of these parameters will be changed manually, and I wanted to run the program based on these updated parameters without restarting Julia (trying to avoid loading packages). Looks like the variable manipulations are not being carried out even when I use `Revise.jl`

Anyone have any suggestions

A MWE below

```julia
module Mymodule
x = 1
y = x*10
mygreeting() = "Hello, world!"

export x, y, mygreeting
end

```

The main file

```julia
using Revise
includet("MyFile.jl")
using Main.Mymodule

#accesssing from workspace directly
println(x)  
println(y) #Does not update it's value
mygreeting()

println(Main.Mymodule.x)
println(Main.Mymodule.y) #Does not update it's value
println(Main.Mymodule.mygreeting())

```

The workflow in question

1. Run the main file
2. Update the module by `x = 2` and `mygreeting() = "Hello, Revised world!"`
3. Run main file again

The variable `x` and function `mygreeting()` gets updated to the new values, but `y` is still showing old value of `10`.  
Is there some way of updating the value in `y`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 10, 2020, 4:23pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/2 "2020-09-10T16:23:10Z")

</div>

This is the behavior I see (v1.5.1)

```julia
julia> module Mymodule;
       x = 1; y = x*10
       export x, y
       end
Main.Mymodule

julia> using Main.Mymodule

julia> println(x)
1

julia> println(y)
10

julia> module Mymodule;
       x = 25; y = x*10
       export x, y
       end
WARNING: replacing module Mymodule.
Main.Mymodule

julia> using Main.Mymodule
WARNING: using Mymodule.x in module Main conflicts with an existing identifier.
WARNING: using Mymodule.y in module Main conflicts with an existing identifier.

julia> println(x)
1

julia> println(y)
10

julia> println(Mymodule.x)
25

julia> println(Mymodule.y)
250

```

The exported names conflict and therefore don’t get updated, but the internal module variables are accessible, and do update. If you start a new session, do you still see the behavior you described? (with or without using include should be the same I think)

---

<div class="post-metadata">

**Author:** ![sandeep\_prakash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandeep_prakash/32/17620_2.png) [@sandeep\_prakash](https://discourse.julialang.org/u/sandeep_prakash)\
**Post date:** [September 10, 2020, 4:31pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/3 "2020-09-10T16:31:12Z")

</div>

> [@tomerarnon](#):
>
> ```julia
> WARNING: using Mymodule.x in module Main conflicts with an existing identifier.
> WARNING: using Mymodule.y in module Main conflicts with an existing identifier.
> 
> ```

I do not get this warning I’m guessing because of Revise.jl. since the module is in a separate file called “MyFile.jl” which revise is watching and updating on the go.

> [@tomerarnon](#):
>
> If you start a new session, do you still see the behavior you described? (with or without using include should be the same I think)

if I restart Julia session - All works as expected (variable j is updated) since Julia reads the file again fresh. I did not want to do it since it requires loading all the packages again which takes some time.

Wanted to check with the experts if I could get away by changing parameters without restarting Julia.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 10, 2020, 4:33pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/4 "2020-09-10T16:33:47Z")

</div>

~~What happens if you explicitly say `using Mymodule` after changing/saving/re-including it?~~

~~Tested it myself with Revise. I see the same behavior as you. I would file an issue on Revise.jl.~~

Actually, this seems like a natural consequence of how Revise works. It doesn’t scan the entire module for inter-variable dependencies (it would have to do this as strings anway, since julia doesn’t store this information in any form; i.e. `y = 10`, not `10*x` as far as julia is concerned) and it doesn’t reinclude all of the source from scratch, as that’s exactly what it’s trying to avoid. Variable capture in functions happens to be by binding, so if you made `y() = 10*x` you should indeed see it change along with x, but otherwise it is independent. A change like `y = 11*x` would redefine y.

I think the takeaway here is to design your global variable interface differently 😅. Maybe as mutable structs, Dicts, etc… Also beware that global variables in julia are bad for performance unless they are `const`. Check out the performance tips of the manual for more info.

---

<div class="post-metadata">

**Author:** ![sandeep\_prakash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandeep_prakash/32/17620_2.png) [@sandeep\_prakash](https://discourse.julialang.org/u/sandeep_prakash)\
**Post date:** [September 10, 2020, 4:48pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/5 "2020-09-10T16:48:19Z")

</div>

Without Revise - I could not find any other way of getting around the name conflict. And having the freedom of using the names directly (without Module.x) was a nice feature that made Revise.jl a nice option

I was unsure if that is the expected behaviour of Revise itself. They only gave examples using functions inside of modules, and not on variables being manipulated like in this MWE.  
I checked by changing `x` into an array, still the variable `y` is not being updated even when calling on lines `includet and using`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 10, 2020, 4:49pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/6 "2020-09-10T16:49:51Z")

</div>

I edited my answer above, I think we were both typing at the same time!

---

<div class="post-metadata">

**Author:** ![sandeep\_prakash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sandeep_prakash/32/17620_2.png) [@sandeep\_prakash](https://discourse.julialang.org/u/sandeep_prakash)\
**Post date:** [September 10, 2020, 5:03pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/7 "2020-09-10T17:03:25Z")

</div>

> [@tomerarnon](#):
>
> I think the takeaway here is to design your global variable interface differently

I agree this might be some of the Bad habits i have from MATLAB.  
The ability to specify main parameters in a separate file and just importing them in functions that i needed is something i must unlearn then 😉

> [@tomerarnon](#):
>
> global variables in julia are bad for performance unless they are `const`

I wanted to avoid global variables, since I do end up using same names in local scopes sometimes (x0 is initial state in one function whereas also initial guess in a different function and context), and the Julian way of inheriting into local scopes scared me off.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 10, 2020, 5:35pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/8 "2020-09-10T17:35:54Z")

</div>

> [@sandeep\_prakash](#):
>
> and the Julian way of inheriting into local scopes scared me off.

Do you mean be using the `global` keyword, or something else?  
Not that I’m advocating for global variables (although if they’re `const`, there is no performance cost, and of course _sometimes_ they’re necessary), but it is very easy to capture globals:

```julia
julia> const α = 11
11

julia> f(x) = x + α
f (generic function with 1 method)

julia> f(1)
12

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 10, 2020, 6:11pm UTC](https://discourse.julialang.org/t/variables-not-recalculating-while-reloading-modules/46396/9 "2020-09-10T18:11:17Z")

</div>

> [@sandeep\_prakash](#):
>
> The variable `x` and function `mygreeting()` gets updated to the new values, but `y` is still showing old value of `10` .  
> Is there some way of updating the value in `y`

The easiest way to get what you want is:

- put your functions in one file, and `includet` it _once_;
- put your computations in a separate file, and `include` it (no “t”) _each time you want to rerun the computations_

You can even use Revise’s `entr` to automatically re-include the computations file any time that file or your code file changes.
