# Problem with using in Submodules

**URL:** <https://discourse.julialang.org/t/problem-with-using-in-submodules/42321>\
**Category:** New to Julia\
**Created:** [June 30, 2020, 7:31pm UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321 "2020-06-30T19:31:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [June 30, 2020, 7:31pm UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321/1 "2020-06-30T19:31:17Z")

</div>

Dear all,  
I have a problem with modules.

I have a file with

```julia
module Root

include("Branch.jl")
include("OtherBranch.jl")
include("Stalk.jl")

using .Branch, .OtherBranch, .Stalk

end

```

Now, in OtherBranch.jl I need something which is defined in Stalk, where I have defined a module called StalkDef, so my code is as follows

```julia
module OtherBranchDef
using ..StalkDef

end

```

but it fails to compile, saying that StalkDef is not defined.  
I tried also with

```julia
module OtherBranchDef
using Root.StalkDef

end

```

but it is not working.  
It seems like it is processing the file from top to bottom, so I would have to move the inclusion of the two files (I cannot, they “use” each other).

How does this work, I think I’m missing something.

Best wishes  
Isaia

---

<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:** [June 30, 2020, 7:47pm UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321/2 "2020-06-30T19:47:07Z")

</div>

> [@Isaia\_Nisoli](#):
>
> It seems like it is processing the file from top to bottom,

Yes, that’s right.

> [@Isaia\_Nisoli](#):
>
> (I cannot, they “use” each other)

That’s the actual problem–you can’t do this. Here’s a simpler non-working example:

```julia
julia> module A
         module B
           using ..C
         end
         module C
           using ..B
         end
       end
ERROR: UndefVarError: C not defined

```

There must be a non-cyclic ordering of your various modules; they cannot depend on each other. If they both need to share some common information (such as a type definition), then you can move the shared information into a new module and have both modules depend on that shared module.

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [July 1, 2020, 12:57pm UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321/3 "2020-07-01T12:57:00Z")

</div>

Oh, thank you! I am trying to refactor the code to avoid this…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 2, 2020, 11:36am UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321/4 "2020-11-02T11:36:04Z")

</div>

@rdeits, I pilfered your nice MWE for

[https://github.com/JuliaLang/julia/pull/38271](https://github.com/JuliaLang/julia/pull/38271)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 2, 2020, 1:19pm UTC](https://discourse.julialang.org/t/problem-with-using-in-submodules/42321/5 "2020-11-02T13:19:15Z")

</div>

It is maybe worth noting that while you may not do this with two “sibling modules” you can do it with “parent and children”:

```julia
julia> module Outer
         module Inner
           import ..Outer
           function i()
             Outer.o()
           end
         end
         import .Inner
         function o()
           Inner.i()
         end
         export o
       end

```

Which compiles and gives a `ERROR: StackOverflowError:` if any of the functions is called.
