# How to import custom module from file without include

**URL:** https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139
**Category:** General Usage
**Tags:** question
**Created:** [June 21, 2022, 6:47pm UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139 "2022-06-21T18:47:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Jack\_N](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_n/32/37265_2.png) [@Jack\_N](https://discourse.julialang.org/u/Jack_N)
#### Post date: [June 21, 2022, 6:47pm UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/1 "2022-06-21T18:47:55Z")

</div>

I made a custom module in a file database.jl called DecisionNode but I cannot seem to find a way to import the DecisionNode module without using the include function, which I do not want to do since I will be using the DecisionNode module in multiple files.

Is there a way to import this custom module without turning that module into a package and then importing that package?

---

<div class="post-metadata">

### Author: ![jonathan-laurent](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@jonathan-laurent](https://discourse.julialang.org/u/jonathan-laurent)
#### Post date: [June 21, 2022, 7:45pm UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/2 "2022-06-21T19:45:52Z")

</div>

You cannot avoid using `include` here. See the manual for more information on how Julia projects/packages are typically organized:

[https://docs.julialang.org/en/v1/manual/modules/](https://docs.julialang.org/en/v1/manual/modules/)

Note that some people in the community (including myself) are considering this an unsatisfactory state of affairs and this has been the topic of many passionate discussions:

- [[ANN] PatModules.jl: a better module system for Julia](https://discourse.julialang.org/t/ann-patmodules-jl-a-better-module-system-for-julia/52226)
- [Implicitly loaded modules in the future?](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646)
- [https://github.com/JuliaLang/julia/issues/4600](https://github.com/JuliaLang/julia/issues/4600)

---

<div class="post-metadata">

### Author: ![Jack\_N](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_n/32/37265_2.png) [@Jack\_N](https://discourse.julialang.org/u/Jack_N)
#### Post date: [June 21, 2022, 7:50pm UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/3 "2022-06-21T19:50:48Z")

</div>

Thank you for your answer! It is surprising that a better solution has not been implemented yet.

---

<div class="post-metadata">

### Author: ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)
#### Post date: [June 22, 2022, 2:09am UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/4 "2022-06-22T02:09:14Z")

</div>

### Confusion

@Jack_N: The frustration/dislike of how “include” works stems from people confusing how Julia uses `include`/`import`/`using` vs how other languages use seemingly similar commands.

### Question

> [@Jack\_N](#):
>
> I cannot seem to find a way to import the DecisionNode module without using the include function, which I do not want to do since I will be using the DecisionNode module in multiple files.

### Short answer

- `include()` does not “import” modules within a file.
- It is probably better to say that julia “compiles” (to a certain extent) the code “inside” whatever module calls the `include()` command.
- It is _ **not** _ the same as Python’s “import statement” (likely why people get confused with it).
- It is also different from C/C++'s use of `#include` - which is typically only for header files.
- C/C++ combines compiled code from multiple .c/.cpp files together using a _ **linker** _ (not the `#include` directive).

### In Python:

```julia-auto
import TopModule.FooModule.BarModule

```

- Parses/“compiles” file `TopModule/FooModule/BarModule.py` (if not already done)
- Makes contents available through symbol `BarModule`.

### In Julia:

- You should include() all your files once in some way (ex: in a master file - ex: `src/TopModule.jl`).
- Get a reference to `BarModule` symbol through `[TopModule.]FooModule.BarModule`.
- See example below

```julia-auto
#src/TopModule.jl:
#------------------------------------------------------------------
module TopModule
    #These modules could be stored in other files & `include()`-ed here, if desired
    module FooModule
        module BarModule
            foo(x) = 3x
        end #BarModule
    end #FooModule

#Add/"compile" contents of Some_other_file.jl:
include("Some_other_file.jl") #Relative to current file (src/TopModule.jl)

end #TopModule

#src/Some_other_file.jl
#------------------------------------------------------------------
#include() statement in `TopModule.py` "compiles" contents of this file INSIDE module: `TopModule`!

import FooModule.BarModule #Get access to BarModule symbol
#alternative way to get `BarModule` symbol:
using FooModule.BarModule #Also imports any symbol that might be "export"ed by BarModule .

FooModule.f(3) #Call some function in FooModule

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 22, 2022, 3:34am UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/5 "2022-06-22T03:34:03Z")

</div>

The `include` solution is not something that needs fixing. @MA_Laforge says it very well above. The misunderstanding of the organization of the code is what causes the “problem”. Essentially, `include` gives the compiler a chance to “see” the code to be compiled. The user then refers to this compiled code through a module (possibly the `Main` module when working in the repl).

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 22, 2022, 4:27am UTC](https://discourse.julialang.org/t/how-to-import-custom-module-from-file-without-include/83139/6 "2022-06-22T04:27:35Z")

</div>

Unlike in Python, a Julia file is not a module (the perks: (1) 1 module can `include` multiple files, (2) 1 file can define multiple modules). If you don’t make a module _exist_ by `include`ing the file containing its `module ... end` code or by `generate`ing a package, how would you `import` it?
