# Change in behavior when loading a module twice

**URL:** <https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370>\
**Category:** New to Julia\
**Tags:** module\
**Created:** [August 19, 2024, 12:35pm UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370 "2024-08-19T12:35:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)\
**Post date:** [August 19, 2024, 12:35pm UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370/1 "2024-08-19T12:35:30Z")

</div>

I have a module `Mod1` stored in a file `Mod1.jl` and a module `Mod2` stored in a file `Mod2.jl`. ‘Mod1’ exports a function `userfun` that uses a function `foo` which is defined in `Mod2`. `Mod2` also defines some new types.

```julia
module Mod1
	export userfun

	include("Mod2.jl")
	using .Mod2

	function userfun()
		foo()
	end
end

```

```julia
module Mod2
	export foo
	
	using InteractiveUtils # for subtypes function
	
	abstract type MyAbstract end
	struct MyStruct <: MyAbstract end
	
	function foo()
		subtypes(MyAbstract)
	end
end

```

If I now write in a fresh REPL

```julia
include("Mod1.jl")
using .Mod1
userfun()

```

I get

```julia
1-element Vector{Any}:
 Main.Mod1.Mod2.MyStruct

```

as expected. But if I now (say, a user accidentaly) repeat the inclusion of the module and call `userfun`

```julia
include("Mod1.jl")
userfun()

```

I get

```julia
Any[]

```

Seemingly `MyAbstract` suddenly has now subtypes. If I afterwards redo `using .Mod1` I get the same empty array.

Can anyone explain why the behavior changes after I `include` the module twice (This is of course not the actual code, I have tried to distill the problem as much as possible)?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 19, 2024, 2:08pm UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370/2 "2024-08-19T14:08:02Z")

</div>

1. You should use packages to load code.
2. My suspicion is that you noe have two abstract `MyAbstract` types but only onr concrete `MyStruct`

---

<div class="post-metadata">

**Author:** ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)\
**Post date:** [August 20, 2024, 5:58am UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370/3 "2024-08-20T05:58:59Z")

</div>

Thank you for the response. Poitn 2. does seem to aggree with the behavior.

Is 1. a general concept: One should not use modules unless it is wrapped into a package? Does a package always ensure that the module is not evaluated twice  
?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 20, 2024, 7:14am UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370/4 "2024-08-20T07:14:15Z")

</div>

To be clear, the problem isn’t loading the module twice (i.e. running `using .Mod1` twice, which would be fine) but including the file twice, which messes up the modules, don’t do it.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 20, 2024, 9:52am UTC](https://discourse.julialang.org/t/change-in-behavior-when-loading-a-module-twice/118370/5 "2024-08-20T09:52:51Z")

</div>

To elaborate:

```julia
julia> include("Mod1.jl")
Main.Mod1

julia> using .Mod1

julia> userfun()
1-element Vector{Any}:
 Main.Mod1.Mod2.MyStruct

julia> using .Mod1

julia> userfun()
1-element Vector{Any}:
 Main.Mod1.Mod2.MyStruct

```

as you can see, just doing `using .Mod1` twice has no effect. When you `include` the file the second time you missed a warning:

```julia
julia> include("Mod1.jl")
WARNING: replacing module Mod1.
Main.Mod1

julia> userfun()
Any[]

```

My guess is that `Main.userfun` is the old `Mod1.userfun`, which doesn’t see the old `Mod1` anymore as it has been overwritten, but you should be able to call `Mod1.userfun` and in fact

```julia
julia> Mod1.userfun()
1-element Vector{Any}:
 Main.Mod1.Mod2.MyStruct

julia> userfun === Mod1.userfun
false

```

The summary is don’t include files twice, actually read the warnings, and only reload modules.

If you’re doing all of this in the REPL for quick development, you may want to look at [`Revise.jl`](https://github.com/timholy/Revise.jl) instead.
