# Need to include one file multiple times, how to avoid warning

**URL:** <https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 12, 2019, 1:55am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439 "2019-08-12T01:55:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 1:55am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/1 "2019-08-12T01:55:44Z")

</div>

I get some warnings when I `include` one public file in several other files in a same package. It is straightforward to include the public file which defines some common functions. The code is as follows.

```julia
# ./src/test_redefine.jl 
module test_redefine

export Dog, my_add 

include("utils.jl") #include utils.jl the first time
include("dog.jl")         
end # module

# ./src/dog.jl
include("utils.jl") #include utils.jl the second time

struct Dog
    name::String
end

# ./src/utils.jl
function my_add(a, b) 
    println(a, b)
end

```

in the test file

```julia
#./test/runtests.jl
using test_redefine
my_add(1,1)

```

Warning is as follows,

```julia
WARNING: Method definition my_add(Any, Any) in module test_redefine at /Users/zhangliye/code/test_redefine/src/utils.jl:2 overwritten at /Users/zhangliye/code/test_redefine/src/utils.jl:2.
11
   Testing test_redefine tests passed 

```

In c/c++, it is very straightforward to define some command functions in a public file and include it in the files using it.

Julia version,

```julia
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 12, 2019, 2:26am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/2 "2019-08-12T02:26:53Z")

</div>

The problem stems from exporting `my_add` from inside the module `test_redefine` back to `Main`.  
When you include `utils.jl` in `Main` for the second time, it conflicts with the exported `my_add`.  
Since both versions of `my_add` are the same, deleting the `export` should do the trick.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 2:54am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/3 "2019-08-12T02:54:25Z")

</div>

Thank you so much for your response. I know the reason for this. I think this is very straightforward to include the public file in all files using it. Why Julia still give the warning? In C++/C we include the head file in files using it, and when compile the code only one function is defined. I want to know if Julia can do the same or just do not give the warning if the source file and function are the same.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [August 12, 2019, 3:02am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/4 "2019-08-12T03:02:29Z")

</div>

Put your “utils” into a module and use it with “using utils”.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 12, 2019, 3:03am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/5 "2019-08-12T03:03:41Z")

</div>

You should not need to include the same file multiple times in Julia, so the answer to your question is “don’t do that”. This is an important difference between the way Julia and C++ are used. Instead, you can do:

```julia
module A

include("B.jl")
using .B

module C
  using ..B
end

end

```

Where B.jl looks like:

```julia
module B
...
end

```

The `using ..B` line lets you load the B module from A inside C without having to include it twice.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [August 12, 2019, 3:08am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/6 "2019-08-12T03:08:01Z")

</div>

Alternatively you can add your current path to your module search path:

```julia
push!(LOAD_PATH, pwd())

```

Then you don’t need any include, just use “using”.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 3:37am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/7 "2019-08-12T03:37:58Z")

</div>

Thank you so much! It can be solved by put the file into one module. However, the code look not so clean. The two version comparison is as follows,  
Version 1: Put the file into one module

```julia
# file1: ./src/test_redefine.jl 
module test_redefine

export Dog, my_add 

include("utils.jl")
using .utils # one more code here
include("dog.jl")
end # module

# file2: ./src/dog.jl
using .utils # changed to us module, the user should pay attention the definition order, `utils` must defined ahead

struct Dog
    name::String
end

# file3: ./src/utils.jl
module utils # one more line code here
export my_add # one more code here

    function my_add(a, b) 
        println("my_add ", a, b)
    end
end # one more code here 

```

Version 2: Just do as in C++/C, include the public head file. The code is more cleaner and more straightforward.

```julia
# file1: ./src/test_redefine.jl 
module test_redefine

export Dog, my_add 

include("utils.jl") #include utils.jl the first time
include("dog.jl")         
end # module

# file2: ./src/dog.jl
include("utils.jl") #include utils.jl the second time

struct Dog
    name::String
end

# file3: ./src/utils.jl
function my_add(a, b) 
    println(a, b)
end

```

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 3:51am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/8 "2019-08-12T03:51:25Z")

</div>

In `utils.jl` I define some enum variables which is used in several files as follows

```julia
# utils.jl
@enum State waiting=1 started=2 arrived=3

```

It is not logically reasonable to put it in a separate module.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [August 12, 2019, 8:01am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/9 "2019-08-12T08:01:09Z")

</div>

Your second way is cleaner. However, I do not think you need to `include("utils.jl")` twice. I prefer modify your first way to

```julia
# file1: ./src/test_redefine.jl
module test_redefine

using Reexport: @reexport # @reexport does what you do `export my_add` there

include("utils.jl")
@reexport using .utils

include("dog.jl")

end # module

```

Is it really unclean? I think it is pretty good.

```julia
# file2: ./src/dog.jl
using .utils # changed to us module, the user should pay attention the definition order, `utils` must defined ahead

export Dog # I prefer to export each type/method at the places where it is defined

struct Dog
    name::String
end

```

The downside is, you cannot run this file directly. Every time you have to `using .test_redefine`, then `Dog` is loaded. I think this is the “correct” way when you are writing a package.

```julia
# file3: ./src/utils.jl
module utils # one more line code here
export my_add # one more code here

function my_add(a, b)
    println("my_add ", a, b)
end

end # one more code here

```

Here I used [Reexport.jl](https://github.com/simonster/Reexport.jl).

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 12, 2019, 11:34am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/10 "2019-08-12T11:34:31Z")

</div>

I don’t see anything wrong with including a simple file that just contains some constants multiple times. Am I missing any issues this could generate?

As for the `reexport` solution: I find it a bit complicated for the use case here. The way I understand it, @zhangliye just wants to export a single object from `utils.jl`. So why not simply:

```julia
module test_redefine

export my_add
include("utils.jl")
include("dog.jl")

end # module

```

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 11:39am UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/11 "2019-08-12T11:39:15Z")

</div>

Thanks for your reply. There is no error, just a lot of warnings. When the project is large, there are too many warnings as follows.

```julia
WARNING: Method definition my_add(Any, Any) in module test_redefine at /Users/zhangliye/code/test_redefine/src/utils.jl:2 overwritten at /Users/zhangliye/code/test_redefine/src/utils.jl:2.
11
   Testing test_redefine tests passed 

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 12, 2019, 12:57pm UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/12 "2019-08-12T12:57:27Z")

</div>

If you really want to stick with your original code structure, you can get rid of the warnings by just not exporting `my_add` twice, i.e., by not exporting it from `test_redefine`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 12, 2019, 1:05pm UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/13 "2019-08-12T13:05:22Z")

</div>

> [@hendri54](#):
>
> I don’t see anything wrong with including a simple file that just contains some constants multiple times. Am I missing any issues this could generate?

It’s definitely not recommended in Julia. Any types or functions defined in that file will be defined _twice_, and those two definitions will be completely different, despite having the same name. This can lead to confusing situations. For some examples of real-world problems caused by multiple includes, see:

- [Trouble importing a module that depends on another globally imported module](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482)
- [Local Modules with Conflicting Names and Module Management](https://discourse.julialang.org/t/local-modules-with-conflicting-names-and-module-management/26942)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 12, 2019, 1:06pm UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/14 "2019-08-12T13:06:36Z")

</div>

> When the project is large, there are too many warnings as follows.

Yes, these warnings are important, and they are trying to tell you that by calling `include()` on the same file multiple times you are re-defining the same function over and over, which is at best wasteful and at worst can lead to problems like [Trouble importing a module that depends on another globally imported module](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482)

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [August 12, 2019, 1:08pm UTC](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/15 "2019-08-12T13:08:32Z")

</div>

Thank you so much! Extremely useful information!
