# Referencing the same module from multiple files

**URL:** https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775
**Category:** New to Julia
**Tags:** question, module, modules
**Created:** [March 12, 2022, 2:44am UTC](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775 "2022-03-12T02:44:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![knttnk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/knttnk/32/38861_2.png) [@knttnk](https://discourse.julialang.org/u/knttnk)
#### Post date: [March 12, 2022, 2:44am UTC](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775/1 "2022-03-12T02:44:48Z")

</div>

# Reproducible code

I created 3 files like

```julia-auto
# useful.jl
module Useful # issue
struct UsefulStruct 
    data
end
end

```

```julia-auto
# create.jl
module Create
include("useful.jl")

function create()::Useful.UsefulStruct
    Useful.UsefulStruct(1)
end
end

```

```julia-auto
# calculate.jl
module Calculate
include("useful.jl")
include("create.jl")

calculate(s::Useful.UsefulStruct) = s.data * 2
end

```

in a directory, ran

```julia-auto
include("create.jl")
include("calculate.jl")

Calculate.calculate(
    Create.create()
)

```

and got following error.

```julia-auto
ERROR: LoadError: MethodError: no method matching calculate(::Main.Create.Useful.UsefulStruct)
Closest candidates are:
  calculate(::Main.Calculate.Useful.UsefulStruct) at /...

```

# Question

I thought I was using the same `UsefulStruct`, but it is recognized as something different.  
What should I do if I want to do these things?

---

<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: [March 12, 2022, 3:29am UTC](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775/2 "2022-03-12T03:29:05Z")

</div>

Welcome to our community! 🎊

This problem is frequent among novice Julia programmers and was already asked multiple times in this forum. (But I concede that the search utility of Discourse **never** finds any useful results, even when I know they exist.) See:

> [@MethodError: no method matching, but the method exists](https://discourse.julialang.org/t/methoderror-no-method-matching-but-the-method-exists/54767):
>
> I’m getting the following error: ERROR: MethodError: no method matching choose(::Main.Sim.ModelBasedAgent, ::Int64) Closest candidates are: choose(::Main.Sim.ModelFreeAgent, ::Int64) at /home/xxxxx/Projects/simulateagents.jl:57 choose(::Main.Sim.ModelBasedAgent, ::Int64) at /home/xxxxx/Projects/simulateagents.jl:53 But there is a matching method. What’s going on?

> [@Dependencies of src files inside a package](https://discourse.julialang.org/t/dependencies-of-src-files-inside-a-package/43144/3):
>
> Technically, I agree with @rdeits. However, I’m not sure that’s really the solution you are looking for. My experience I have experienced the same confusion myself, and I have noticed that others seem to be stuck in the same mindset. I think it might stem from our ingrained C/C++'s include/header file methodology (assuming that’s your background). In most cases, I don’t really want to break things up into different modules - especially inside a single package. Typically, when I need to bre…

Basically, the rule is: _include **once** and import whenever necessary_.

Your topmost module should **include** all files a single time and you should use `import`/`using` relative to it (`using .MyModule` refers to a module defined in the current module, `using ..MyModule` refers to a module define in the parent module, i.e., a sibling module, and you can do something like `using ...MyModule.A.B` to go to the parent of the parent and then go down to a specific submodule). In your case, if you `include` `Useful` and `Create` inside `Calculate` you want to replace the `include("useful.jl")` inside `Create` by `import ..Useful`

---

<div class="post-metadata">

### Author: ![knttnk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/knttnk/32/38861_2.png) [@knttnk](https://discourse.julialang.org/u/knttnk)
#### Post date: [March 13, 2022, 2:49am UTC](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775/3 "2022-03-13T02:49:43Z")

</div>

Thank you for your polite and kind reply!  
It solved my problem.
