# Trouble importing a module that depends on another globally imported module

**URL:** <https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482>\
**Category:** General Usage\
**Tags:** type, module\
**Created:** [July 17, 2019, 10:17pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482 "2019-07-17T22:17:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [July 17, 2019, 10:17pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/1 "2019-07-17T22:17:01Z")

</div>

I have a project which is structured like:

```julia
Connect4Solver:
->Connect4Board.jl
->MCTS.jl

```

`Connect4Board.jl` defines a module `Connect4Board` which defines a type `Board`.  
`MCTS.jl` defines a module `MCTS` which imports `Connect4Board` as follows:

```julia
include("Connect4Board.jl")
using(.Connect4Board)

```

After importing both of these modules into the REPL the same way (changing just the name for the `MCTS` import), I am able to create new instances of the `Board` type.  
However, when I go to pass an instance of this into functions declared in the `MCTS` module, I get the following error:

```julia
ERROR: MethodError: no method matching MCTSpickmove(::Board)
Closest candidates are:
  MCTSpickmove(::Main.MCTS.Connect4Board.Board, ::Float32) at D:\Users\Zachary\Julia Projects\Connect4Solver\MCTS.jl:67
  MCTSpickmove(::Main.MCTS.Connect4Board.Board) at D:\Users\Zachary\Julia Projects\Connect4Solver\MCTS.jl:67
Stacktrace:
 [1] top-level scope at none:0

```

I am able to avoid this error by exporting the necessary parts of `Connect4Board` from `MCTS` instead of directly importing both into the REPL, but that seems pretty sloppy. Is there a better way to get around this?

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [July 18, 2019, 2:53am UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/2 "2019-07-18T02:53:47Z")

</div>

Perhaps you’re defining the module multiple times by using `include`? This can be very confusing because it redefines a new module with the same name, and is rarely what you want. Instead you should add your module to the load path and it will be found automatically by `using`, or at least have a single file which `include`s both modules exactly once.

Consider the following sad and confusing story-in-code:

```julia
julia> module A
           struct B end
           foo(b::B) = "something"
       end
Main.A

julia> b = A.B()
Main.A.B()

julia> A.foo(b)
"something"

julia> OldA = A
Main.A

julia> module A
           struct B end
           foo(b::B) = "something"
       end
WARNING: replacing module A.
Main.A

julia> A.foo(b)
ERROR: MethodError: no method matching foo(::Main.A.B)
Closest candidates are:
  foo(::Main.A.B) at REPL[11]:3
Stacktrace:
 [1] top-level scope at none:0

julia> b isa A.B
false

julia> b isa OldA.B
true

```

Note the “WARNING: replacing module A”. If you ever see this kind of thing, it means you’ve redefined a module with the same name as an existing module. Any variable `b` which you had hanging around from the old module is still available, but it has the type `OldA.B` (printed as `A.B`) rather than `A.B` (also printed as `A.B`!). This might be the problem you’re having if you’re calling `include` multiple times on a file with the module code inside.

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [July 18, 2019, 11:31am UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/3 "2019-07-18T11:31:37Z")

</div>

I see what you’re saying here, but I’m not importing them both in the same namespace. It never gives a warning like that either.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [July 19, 2019, 4:04am UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/4 "2019-07-19T04:04:18Z")

</div>

Fair enough, I was just taking a guess based on incomplete information. If you can provide a link to the code or a reduced version of the code which is causing the problem then it will be easier to understand your confusion. See [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

---

<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:** [July 19, 2019, 2:33pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/5 "2019-07-19T14:33:33Z")

</div>

> [@zachmatson](#):
>
> After importing both of these modules into the REPL the same way

How exactly are you doing this? If you are `include()`-ing both files, then this is exactly the problem that @c42f described: you have two copies of your Connect4Board module, one of which from the `include()` at the REPL and another from the `include()` inside `MCTS.jl`.

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [July 19, 2019, 3:18pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/6 "2019-07-19T15:18:04Z")

</div>

Yeah just identical include and using. If this is the issue, how would I avoid that? For example, with MCTS.jl depending on the Connect4Board module, it can’t compile when that module isn’t imported.

---

<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:** [July 19, 2019, 3:26pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/7 "2019-07-19T15:26:51Z")

</div>

If you do:

```julia
push!(LOAD_PATH, "/path/to/your/src")

```

then you should be able to do:

```julia
using YourPackageName

```

without needing to `include()` anything from the REPL.

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [July 19, 2019, 4:38pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/8 "2019-07-19T16:38:23Z")

</div>

I was a bit wary of changing `LOAD_PATH` for something I’m still testing, but I suppose there is always removing from it too, so I guess this is the best way. Thank you!

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [July 19, 2019, 9:01pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/9 "2019-07-19T21:01:46Z")

</div>

Perhaps you didn’t see the warning because you were including one module inside the other, and then including it again inside `Main`? Then you’d get `Main.A` and `Main.B.A` where the names don’t clash (and you get no warning), but it’s essentially the problem I described above.

[Edit: I see @rdeits already suggested this explanation]

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [July 19, 2019, 9:53pm UTC](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/10 "2019-07-19T21:53:16Z")

</div>

Yeah it looks like you were right. Thanks for the help!
