# Reload a module from within a while loop

**URL:** <https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 25, 2016, 1:45pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560 "2016-11-25T13:45:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [November 25, 2016, 1:45pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/1 "2016-11-25T13:45:50Z")

</div>

I wish to reload a module based upon some user input.

The following works…

```julia
while true
    import mod
    readline()
    reload("mod")
    mod.test()
end

```

but this fails to reload the module…

```julia
import mod

while true
    readline()
    reload("mod")
    mod.test()
end

```

I assume due to some scoping rules. How can one achieve the same functionality without importing inside the while loop?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [November 25, 2016, 3:00pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/2 "2016-11-25T15:00:21Z")

</div>

`import` and `reload` (or rather `require` which does the real work) involve convoluted logic. Unless you need some feature of packages, you could do this (without the `import`):

```julia
while true
    readline()
    include("mod.jl")
    mod.test()
end

```

which exploits the fact that `include` operates at global scope (so module definitions are ok).

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [November 25, 2016, 3:02pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/3 "2016-11-25T15:02:03Z")

</div>

See also: [GitHub - cstjean/ClobberingReload.jl: reload for interactive work + Autoreload successor](https://github.com/cstjean/ClobberingReload.jl)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 26, 2016, 2:44pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/4 "2016-11-26T14:44:34Z")

</div>

`ClobberingReload.creload` might work, but the fundamental issue is likely that `mod.test` gets hard-linked by the compiler, so maybe you should try:

```julia
while true
    readline()
    reload("mod") # or creload
    @eval mod.test()
end

```

It will force the compiler to recompile the `mod.test` call at every iteration of the loop.

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [November 26, 2016, 11:03pm UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/5 "2016-11-26T23:03:08Z")

</div>

I do use ClobberingReload, but on this occasion I had some difficulties (`@ __FILE__ ` statement returning nothing when evaluated during reloading). I will open an issue if I can’t figure it out.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 27, 2016, 4:21am UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/6 "2016-11-27T04:21:12Z")

</div>

> [@samuelpowell](#):
>
> I will open an issue if I can’t figure it out.

Please do. The `@ __FILE__ ` problem might be [this issue](https://github.com/cstjean/ClobberingReload.jl/issues/3), which is potentially solved on master. I’ll tag it next week.

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [November 28, 2016, 9:21am UTC](https://discourse.julialang.org/t/reload-a-module-from-within-a-while-loop/560/7 "2016-11-28T09:21:06Z")

</div>

Yes, this is fixed on master. Thank you for your help.
