# How to arrange a package with submodules and multiple files?

**URL:** <https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 10, 2019, 4:48pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709 "2019-03-10T16:48:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gu\_Junhua](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Gu\_Junhua](https://discourse.julialang.org/u/Gu_Junhua)\
**Post date:** [March 10, 2019, 4:48pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/1 "2019-03-10T16:48:49Z")

</div>

Say I’m writing a package `P`, which contains two sub-modules `M1` and `M2`. Both submodules share some common codes, so I extract the common codes and wrap it in another sub module `C`

I decide to split the codes into four files:

1. the root module file P.jl

```julia
module P
include("M1.jl")
include("M2.jl")
end

```

1. M1.jl

```julia
module M1
include("C.jl")
...
end

```

1. M2.jl

```julia
module M2
include("C.jl")
...
end

```

1. C.jl

```julia
module C
...
end

```

Then the topology of the package `P` becomes  
P-\>M1-\>C  
P-\>M2-\>C

And the content of C.jl appears two times in the final P.jl (?). Seems to be rather noisy.

Is there a better method to arrange the files?

Or is there any guides about how to arrange the files?

Thanks.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 10, 2019, 5:05pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/2 "2019-03-10T17:05:01Z")

</div>

The first thing to do is to once more question yourself whether you really benefit from the split into submodules. If the answer is yes, maybe try something with the structure

P.jl:

```julia
module P
include("C.jl")
include("M1.jl")
include("M2.jl")
end

```

M1.jl:

```julia
module M1
using ..C
end

```

M2.jl:

```julia
module M2
using ..C
end

```

C.jl:

```julia
module C
end

```

---

<div class="post-metadata">

**Author:** ![asprionj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asprionj/32/6856_2.png) [@asprionj](https://discourse.julialang.org/u/asprionj)\
**Post date:** [March 10, 2019, 11:41pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/3 "2019-03-10T23:41:45Z")

</div>

Any reason not to have the common parts directly within module P?  
(`P.jl` as proposed by @GunnarFarneback, but omit the `using ..C` statements in the module files and the `module` block in `C.jl`.)

---

<div class="post-metadata">

**Author:** ![congvan](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@congvan](https://discourse.julialang.org/u/congvan)\
**Post date:** [March 11, 2019, 2:11am UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/4 "2019-03-11T02:11:03Z")

</div>

Including C.jl two times is not only noisy, but may also cause confusion or bad error, for example, if C.jl has type “TestType”, and one includes “C.jl” in “M1.jl” and “M2.jl” then “TestType” is not unique in “M1.jl” and “M2.jl” and we have two different “TestType”.

In my experience, I usually do all the “include” in the outter most module and the inner modules will only use “using .“ where the number of dots depends on the hierarchy.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [March 11, 2019, 10:50pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/5 "2019-03-11T22:50:52Z")

</div>

> [@asprionj](#):
>
> Any reason not to have the common parts directly within module P?  
> ( `P.jl` as proposed by @GunnarFarneback, but omit the `using ..C` statements in the module files and the `module` block in `C.jl` .)

This is a good question.  
Do submodules have access to the enclosing scope of parent modules?  
If so, what are the pros/cons of this vs including `module C`?

---

<div class="post-metadata">

**Author:** ![Gu\_Junhua](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Gu\_Junhua](https://discourse.julialang.org/u/Gu_Junhua)\
**Post date:** [March 12, 2019, 2:43am UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/6 "2019-03-12T02:43:22Z")

</div>

Wrapping some common codes into a submodule can reduce the name pollution to namespace. And sometimes, the common codes are rather large.

---

<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:** [March 12, 2019, 10:17pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/7 "2019-03-12T22:17:29Z")

</div>

> [@congvan](#):
>
> Including C.jl two times is not only noisy, but may also cause confusion or bad error, for example, if C.jl has type “TestType”, and one includes “C.jl” in “M1.jl” and “M2.jl” then “TestType” is not unique in “M1.jl” and “M2.jl” and we have two different “TestType”.

For an example of exactly the kind of problem that @congvan is correctly pointing out, see this recent post: [I'm confused with types names](https://discourse.julialang.org/t/im-confused-with-types-names/21795) . Including a file multiple times will result in different types with the same name and is likely to create all kinds of confusion.

---

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [January 16, 2026, 10:17pm UTC](https://discourse.julialang.org/t/how-to-arrange-a-package-with-submodules-and-multiple-files/21709/8 "2026-01-16T22:17:14Z")

</div>

What does `using ..C` mean? I know only `using C` (`C` is a module in another package) and `using .C` (`C` is an internal module in the same package).
