# How to include my module file as a package?

**URL:** https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682
**Category:** General Usage
**Tags:** question, package, module
**Created:** [February 2, 2022, 7:43pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682 "2022-02-02T19:43:52Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [February 2, 2022, 7:43pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/1 "2022-02-02T19:43:52Z")

</div>

I have some user-defined structures and I put them in `Module` file.

```julia
module name_module
# here all structures
end

```

I want to import my structures from my module as below (as a package), however, I have an error. Any idea to solve that?

```julia
using .name_module
ERROR: LoadError: UndefVarError: name_module not defined

```

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [February 2, 2022, 7:52pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/2 "2022-02-02T19:52:42Z")

</div>

```julia
julia> include("Module.jl")

julia> using .name_module

```

* * *

Oops, I didn’t look at the error you were getting carefully enough. We probably need more info on how `name_module` is using `ModuleClasses` to be able to help.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [February 2, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/3 "2022-02-02T19:56:02Z")

</div>

@StevenWhitaker thanks!  
Yes, I was doing this. But as you know to get ride of the compilation time of the code, I have to run it twice. So, when I run it for the second time, it raises a warning of replacing the module due to the `include("name_module.jl")`. So, I want to get ride of this issue?

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [February 2, 2022, 8:02pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/4 "2022-02-02T20:02:11Z")

</div>

Running `include("name_module.jl")` shouldn’t give an error the second time unless it threw an error the first time if I understand correctly; replacing the module will issue a warning, but not an error. There is something else happening, related to `ModuleClasses` based on the error message you posted above. It would probably help to post a minimum working example (MWE) to illustrate how you are getting your error.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [February 2, 2022, 8:04pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/5 "2022-02-02T20:04:36Z")

</div>

@StevenWhitaker Thanks!

Yes, right! I am having a warning (not error) of replacing the module. Any way to avoid it?

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [February 3, 2022, 3:06pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/6 "2022-02-03T15:06:22Z")

</div>

You will always get a warning whenever replacing a module.

I’m confused as to what help you want in this post. Your original post shows an error about `ModuleClasses` not being defined, but now you are consistently asking for help avoiding a warning about replacing a module. These two issues seem unrelated to each other.

Again, it would probably help to include a [MWE](https://en.wikipedia.org/wiki/Minimal_working_example#:~:text=From%20Wikipedia%2C%20the%20free%20encyclopedia%20In%20computing%2C%20a,bug%20or%20problem%20to%20be%20demonstrated%20and%20reproduced.).

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [February 3, 2022, 3:19pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/7 "2022-02-03T15:19:20Z")

</div>

I am sorry if two things are missed up. My code is as below. I run it by clicking `Execute active file in REPL` via VS Code. For the first run, it is ok, but when I run the code again it shows the warning of replacing the module. So, here what is the modifications that I need to do on the code to avoid this warning?

```julia
include("name_module.jl");
using .name_module
# rest of the code

```

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [February 3, 2022, 3:36pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/8 "2022-02-03T15:36:10Z")

</div>

> [@Amro](#):
>
> So, here what is the modifications that I need to do on the code to avoid this warning?

> [@StevenWhitaker](#):
>
> You will always get a warning whenever replacing a module.

If you want to avoid the warning, you can’t `include` your module again. So you should run `include("name_module.jl")` once in the active Julia session (not in a script that you will run over and over).

---

<div class="post-metadata">

### Author: ![jdad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdad/32/4739_2.png) [@jdad](https://discourse.julialang.org/u/jdad)
#### Post date: [February 3, 2022, 5:41pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/9 "2022-02-03T17:41:56Z")

</div>

You could try FromFile ?

```julia
]add FromFile
@from "name_module.jl" using name_module

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [February 3, 2022, 8:39pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/10 "2022-02-03T20:39:43Z")

</div>

It gave me warnings and errors related to my structures, which were not shown before

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 3, 2022, 9:13pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/11 "2022-02-03T21:13:50Z")

</div>

> [@Amro](#):
>
> I want to import my structures from my module as below (as a package), however, I have an error. Any idea to solve that?

I would just [create an actual package](https://pkgdocs.julialang.org/v1/creating-packages/) (you don’t have to register it publicly); it’s the best way to organize and re-use any nontrivial amount of code in the long run. See also

> [@Best practise: organising code in Julia](https://discourse.julialang.org/t/best-practise-organising-code-in-julia/74362/2):
>
> See many previous discussions: [Pros & Cons of using modules vs plain include?](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694) and [Single module vs. submodules in a project](https://discourse.julialang.org/t/single-module-vs-submodules-in-a-project/55361) and [Large programs: structuring modules & include such that to increase performance and readability](https://discourse.julialang.org/t/large-programs-structuring-modules-include-such-that-to-increase-performance-and-readability/29102) and [What is the preferred way to use multiple files?](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687) TLDR: Start with a single module in a single package, split into as many files as you want, and refactor as needed later on. [Create at least one package](https://pkgdocs.julialang.org/v1/creating-packages/) for any major project, so that you can benefit from the Julia pack…

(With your code in a package, [use Revise.jl](https://github.com/timholy/Revise.jl) — then your files will automatically be re-loaded as needed when you edit the files during development.)

---

<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: [February 6, 2022, 8:17pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/12 "2022-02-06T20:17:14Z")

</div>

@Amro:  
To start off, I should first mention my agreement with @stevengj 's previous post. It is highly likely that you would benefit overall by creating an actual package to house your new code.

Having said that, I noticed there being much confusion regarding the differences between `include` and `using`/`import`. I think it is worth explaining these differences so you can avoid pitfalls like the one your issue has hilighted.

### `include()`: What does it do?

Applying terminology typical programmers seem more familiar with: we could almost say that:  
 → `include("some_file_with_code.jl")`  
causes Julia to “compile” `some_file_with_code.jl` — inline with said call (i.e. at the location/time the compiler reaches your call to `include()`).

_ **Sidenote:** _

> However, if I am to use _ **more accurate terminology** _, I believe you should say that the Julia parser “parses” that file, and performs code lowering in order to store various symbols, modules, functions, etc. into a complex tree of data structures… Because what is technically known as the “compilation process” actually happens at a later time.

Point being, the code you `include()` is available for use _ **immediately** _ after the call. Upon `inclu`-sion, Julia generates the appropriate symbols, and makes them available to your current scope…

And if you are wondering, calling:  
`julia> include("some_file_with_code.jl")`  
from the Julia prompt makes the newly “lowered” code available in the module named \>\>`Main`\<\<.

### `using`: What does it do?

In the case where we are dealing with pure modules (not full-blown packages), `using SomeModuleSymbol` gets Julia to _**import a symbol (here: your module’s name) into the current namespace**_.

Note that it would also automatically import any symbols that have been marked for exporting using “`export` statements” (had you included any inside your module).

### “Ok. So what’s wrong with my code?” (paraphrasing)

> [@Amro](#):
>
> ```julia-auto
> include("name_module.jl");
> using .name_module
> # rest of the code
> 
> ```

First off, when you: `include("name_module.jl")`, Julia does the whole code-lowering thing, and makes “`name_module`” available in your namespace.

Then, when you call:

```julia-auto
using .name_module

```

Julia tries to re-import “`name_module`” that is _ **already available** _ in said namespace.

### So what do I do to fix this?

There is simply no need for the “`using .name_module`” command in your example.

Instead, you can immediately start using the module that was made available by calling “`include("name_module.jl")`”, as done here:

```julia-auto
include("name_module.jl");
#using .name_module #Nope: Will re-import already existing symbol

#Just start using the module that is now available:
name_module.run_something_inside_module()

# rest of the code

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 17, 2022, 10:11pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/13 "2022-03-17T22:11:17Z")

</div>

Thanks for your explanation. I am using `using .name_module` to allow using the functions in the module directly without writing `name_module.` before the functions. Can I do that also without writing `using .name_module`?

---

<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 18, 2022, 12:17am UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/14 "2022-03-18T00:17:16Z")

</div>

Probably not. You can however use a nickname, if this helps you in any way.

```julia
import .name_module
const M = name_module
M.my_function(...)

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 18, 2022, 1:03pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/15 "2022-03-18T13:03:21Z")

</div>

Thanks for your reply. So, here eventually do I need also to put `include("name_module.jl");` because this is a user-defined model as below?

```julia
include("name_module.jl");
import .name_module
const M = name_module
M.my_function(...)

```

---

<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 18, 2022, 1:52pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/16 "2022-03-18T13:52:11Z")

</div>

If it is in a file called `name_module.jl`, then yes, but you can define as many modules inside another modules, as many you want per file.

Just note that you should **include** every file just once in your whole project/package, and then if multiple of your modules need the `include`d module then they should **import** (or call **using** ) passing the relative path in the module hierarchy ( **not** the relative path in the filesystem).

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 18, 2022, 2:03pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/17 "2022-03-18T14:03:29Z")

</div>

> [@Henrique\_Becker](#):
>
> Just note that you should **include** every file just once in your whole project/package, and then if multiple of your modules need the `include` d module then they should **import** (or call **using** ) passing the relative path in the module hierarchy ( **not** the relative path in the filesystem).

Thank you very much. Can you give me an example here?

---

<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 18, 2022, 2:56pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/18 "2022-03-18T14:56:10Z")

</div>

See this previous recent thread: [Referencing the same module from multiple files](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775)

---

<div class="post-metadata">

### Author: ![jdad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdad/32/4739_2.png) [@jdad](https://discourse.julialang.org/u/jdad)
#### Post date: [March 18, 2022, 7:02pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/19 "2022-03-18T19:02:07Z")

</div>

A possible quick-and-dirty fix ?

```julia
if !@isdefined name_module; include("name_module.jl"); using .name_module;end

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 18, 2022, 7:04pm UTC](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682/20 "2022-03-18T19:04:42Z")

</div>

Thank you very much. I will try it.

[Next page](https://discourse.julialang.org/t/how-to-include-my-module-file-as-a-package/75682.md?page=2)
