# Pros & Cons of using modules vs plain include?

**URL:** https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694
**Category:** General Usage
**Tags:** modules, code-organization
**Created:** [September 8, 2018, 5:54am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694 "2018-09-08T05:54:38Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)
#### Post date: [September 8, 2018, 5:54am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/1 "2018-09-08T05:54:38Z")

</div>

I would like to understand the two ways of structuring and running scripts:

1. put the scripts in modules, and use ‘using MyModule’ to run them;
2. not put the scripts in any module, and use ‘include(“myfile.jl”)’ to run them.

So scripts in modules get precompiled, and scripts not in modules don’t. Does that make scripts in modules start up faster?

Doing ‘using MyModule’ multiple times does not seem to matter, but doing ‘include(“myfile.jl”)’ multiple times does seem to get some warnings. And, each ‘include’ does seem to increase julia’s memory usage, doesn’t it?

After you edit parts of a module in some way, you can’t simply do ‘using MyModule’, you have to restart julia, which slows down the next run of the script because of loading/compiling. In certain cases which I can’t specify, even the magic of Revise does not help.

And if you don’t use modules, you can keep doing (except when you modified the fields of certain composite types) ‘include(“myfile.jl”)’ until julia’s memory usage gets very high, at which time you need to restart julia.

And So on,…  
So, what really are the pros and cons of the two ways?

---

<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: [September 8, 2018, 6:08am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/2 "2018-09-08T06:08:57Z")

</div>

Use _projects_, which are modules + `Project.toml` + `Manifest.toml`. No need to `dev` them, just `activate` when you are working on a problem (either with `pkg"activate /path/to/project"`, or `julia --project=path`).

This will give you

1. the ability to use Revise.jl (a big win, hard to overemphasize its importance),
2. a consistent environment that is always reproducible and only changes when you want it to (put `Manifest.toml` in version control),
3. precompilation of the project and all of its dependencies (faster startup).

Don’t use single-file scripts for anything else than short throwaway code. Generating and using projects is so simple (`pkg> generate path`) that it is a no-brainer to do it.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [September 8, 2018, 8:56am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/3 "2018-09-08T08:56:45Z")

</div>

On a related note that would prevent this solution. What if a module is used as a namespace, rather than a module per se, i.e. isn’t standalone and can’t be made into a package?

Is the answer just to not use modules incorrectly?

---

<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: [September 8, 2018, 9:09am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/4 "2018-09-08T09:09:59Z")

</div>

> [@jcook](#):
>
> What if a module is used as a namespace, rather than a module per se, i.e. isn’t standalone and can’t be made into a package?

I am not sure I understand. Can you clarify what you mean by this, perhaps with an example?

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [September 8, 2018, 9:33am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/5 "2018-09-08T09:33:31Z")

</div>

Lets say we have a genuine package `Foo.jl` with code inside `module Foo`, and it has a couple of files `Bar.jl` an `Baz.jl`. `Bar.jl` and `Baz.jl` are included into `Foo`. The problem arises because there in their respective files there is a `module Bar` and a `module Baz`, which only serve to make it abundantly clear that that `Baz` stuff is well separated from `Bar` stuff, and also for making functions “private” etc. The code would work just as well without modules `Bar` and `Baz` because they’re really only namespaces for keeping code tidy. As it stands it’s not possible to make a `Bar` or a `Baz` package, and as such do they represent misuses of `modules`?

---

<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: [September 8, 2018, 9:34am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/6 "2018-09-08T09:34:48Z")

</div>

How do you load `Bar` and `Baz`?

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [September 8, 2018, 9:45am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/7 "2018-09-08T09:45:32Z")

</div>

You may be diving deeper than the pool. It should not matter whether a module is intended to provide some computational facility or to be a gathering umbrella.  
`module M; export solve; function solve(x) ... end # module`  
`module M; export insideofm; const insideofm = 12; .. end # module`  
To use the former, `using M; ... solve(x)`.  
To use the latter, `import M; M.insideofm`  
or, `using M:insideofm; insideofm`

---

<div class="post-metadata">

### Author: ![JesperMartinsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jespermartinsson/32/34098_2.png) [@JesperMartinsson](https://discourse.julialang.org/u/JesperMartinsson)
#### Post date: [September 8, 2018, 9:45am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/8 "2018-09-08T09:45:33Z")

</div>

Very interesting question and answer. I have a difficulty finding these tricks in the documentations. Is there a beginner tutorial/documentation on how to setup projects?

---

<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: [September 8, 2018, 9:55am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/9 "2018-09-08T09:55:55Z")

</div>

Pkg3 is [documented](https://julialang.github.io/Pkg.jl/latest/), but this is WIP. I would expect that a lot of the tooling and the tutorials will catch up within a few months.

---

<div class="post-metadata">

### Author: ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)
#### Post date: [September 8, 2018, 10:00am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/10 "2018-09-08T10:00:33Z")

</div>

After you edit parts of a module in some way, you can’t simply do ‘using MyModule’, you have to restart julia, which slows down the next run of the script because of loading/compiling. In certain cases which I can’t specify, even the magic of Revise does not help.

And if you don’t use modules, you can keep doing (except when you modified the fields of certain composite types) ‘include(“myfile.jl”)’ until julia’s memory usage gets very high, at which time you need to restart julia.

---

<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: [September 8, 2018, 10:09am UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/11 "2018-09-08T10:09:52Z")

</div>

> [@LeoK987](#):
>
> In certain cases which I can’t specify, even the magic of Revise does not help.

AFAIK the only remaining major issue is [type redefinitions](https://github.com/timholy/Revise.jl/issues/18), everything else is covered. If not, I opening an issue for Revise may get you closer a solution.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [September 8, 2018, 12:04pm UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/12 "2018-09-08T12:04:44Z")

</div>

With `include` and `import`.

---

<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: [September 8, 2018, 12:06pm UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/13 "2018-09-08T12:06:22Z")

</div>

When using `import`, how is it found? Do you put its directory in the load path?

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [September 8, 2018, 12:39pm UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/14 "2018-09-08T12:39:16Z")

</div>

I include by `include(src/Bar.jl)` and then `import` works. I’m only using 0.6 though if that makes a difference

---

<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: [September 8, 2018, 12:46pm UTC](https://discourse.julialang.org/t/pros-cons-of-using-modules-vs-plain-include/14694/15 "2018-09-08T12:46:00Z")

</div>

I would not call it a misuse of modules then; if this is advantageous for your project setup then go for it. That said, I would just use Requires.jl for conditional functionality.
