# Proper way to use modules

**URL:** https://discourse.julialang.org/t/proper-way-to-use-modules/28909
**Category:** New to Julia
**Tags:** question
**Created:** [September 18, 2019, 6:39pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909 "2019-09-18T18:39:24Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [September 18, 2019, 6:39pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/1 "2019-09-18T18:39:24Z")

</div>

With julia \<=0.6, I used to bring in all my modules in one place and then I would import the loaded modules within other modules as needed with “import …Mod1” as below.

```julia
#my_main.jl

push!(LOAD_PATH, Base.source_dir())

using Mod1
using Mod2

Mod2.bar(5)                                                                                                                             
Mod1.foo(5)

```

```julia
#Mod1.jl

module Mod1

function foo(x)
    println("x+1: ", x+1)
end
                                                                                                                                     
end

```

```julia
#Mod2.jl

module Mod2

import ..Mod1
#or
#import Main.Mod1

function bar(x)
    println("x: ", x)
    Mod1.foo(x)
end

end #module     

```

Then I would run my\_main.jl. In julia 1.0, this gives “WARNING: could not import Mod2.Mod1 into Mod2” and a subsequent “UndefVarError: Mod1 not defined”.

If in Mod2.jl I put “using Mod1” instead of the import “…Mod1”, things work.

Maybe someone can help give me the right mental image of “using”, but this feels weird to me because it feels like I now have a Main.Mod1 and a Mod2.Mod1, which I would expect to effectively be different modules (each getting compiled separately).

So say I made my Mod1.jl as follows:

```julia
module Mod1                                                                                                                             
                                                                                                                                        
counter = 0

function foo(x)
    global counter
    println("counter: ", counter)
    counter += 1                                                                                                                                     
end

end #module

```

It’s not intuitive to me whether there should two instances of counter or one. (There is one.) The output of “julia my\_main.jl” is

```julia
x: 5
counter: 0
counter: 1

```

2 questions:

1. What is a smart, stylistic way to use one module (Mod1) in many other modules? Put “using Mod1” in all the modules? Do something else?
2. Does anyone have an intuitive explanation for what using does with respect to instances of the code it brings in?

Thanks.

---

<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 19, 2019, 5:54am UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/2 "2019-09-19T05:54:14Z")

</div>

1. Don’t use `LOAD_PATH`. Instead, `pkg> add` or `pkg> dev` the relevant packages to the global environment or a custom project. See [https://julialang.github.io/Pkg.jl](https://julialang.github.io/Pkg.jl)

2. If `Mod1` and the module that is using it are submodules of a third module, then `using ..Mod1` is fine. Otherwise, `using Mod1`.

3. Code loading is documented: [Code Loading · The Julia Language](https://docs.julialang.org/en/v1/manual/code-loading/) but I would recommend becoming familiar with the Pkg docs I linked above first.

---

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [September 19, 2019, 3:22pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/3 "2019-09-19T15:22:34Z")

</div>

Thanks @Tamas_Papp.

---

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [September 19, 2019, 4:10pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/4 "2019-09-19T16:10:00Z")

</div>

So @Tamas_Papp, are you suggesting I would make every module its own package?

The way I’ve created the modules, there are many and some are fairly small, so I think this approach would add quite a bit of overhead (e.g., a package directory for each module). But is that the julia style? Each module is a package, so you probably don’t want a lot of small modules unless there is a real reason for it?

---

<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 19, 2019, 4:27pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/5 "2019-09-19T16:27:42Z")

</div>

That mostly depends on whether the modules are useful as self-contained collections of code for a particular purpose. Otherwise, use submodules, or if they are really tiny then just use a single module.

I generally put things in a single module and use submodules for medium-sized projects only, and/or split into other packages when it makes sense. YMMV. You are basically free to do what you like, Julia supports a wide range of choices.

---

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [September 19, 2019, 5:48pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/6 "2019-09-19T17:48:06Z")

</div>

@Tamas_Papp I’m hung up on your suggestion 1. In my case, my entire body of code could be a project, but I don’t think it makes sense to make any of the modules a project themselves (per your “That mostly depends on whether the modules are useful as self-contained collections of code”). The module files are not all in the same directory (and I want them to continue to be organized in separate directories), so I need to specify those directories. Do you do that in the Pkg configuration somewhere? (I’m not seeing it.) Thanks.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [September 19, 2019, 5:52pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/7 "2019-09-19T17:52:39Z")

</div>

Subdirectories of `src` are useful for this kind of organization.

---

<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 19, 2019, 6:09pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/8 "2019-09-19T18:09:37Z")

</div>

In your `src/MyModule.jl` (relative to `Project.toml`) you could have

```julia
module MyModule

include("../that/file/wherever_it_is.jl")
include("../the_other/file/somewhere_else.jl")

end

```

which is non-standard but should work.

---

<div class="post-metadata">

### Author: ![rwgardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwgardner/32/10344_2.png) [@rwgardner](https://discourse.julialang.org/u/rwgardner)
#### Post date: [September 19, 2019, 6:25pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/9 "2019-09-19T18:25:54Z")

</div>

Thanks. I get the include option. …was just wondering if there was some better way of doing that in trying to understand what you were suggesting.

An advantage of

```julia
push!(LOAD_PATH, "../that/file")
push!(LOAD_PATH, "../the_other/file")

using wherever_it_is
using somewhere_else

```

compared to what you wrote (assuming they are modules), is compilations of wherever\_it\_is and somewhere\_else will potentially be cached, right?

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [April 10, 2020, 8:33pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/10 "2020-04-10T20:33:18Z")

</div>

This is the only thing that actually seems to work. But, there are folks that say “make a package…” But making a package is a very sticky wicket indeed.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [April 10, 2020, 9:07pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/11 "2020-04-10T21:07:00Z")

</div>

I disagree. I’ve created like 5 packages in the past few days. Each time it goes like this:

```julia
using PkgSkeleton
PkgSkeleton.generate("/paththatdoesnotalreadyexist/mynewpackage")

```

Done. Package created. You can do the same thing without using PkgSkeleton.jl, but this way tt has the recommended directory structure, documentation mostly ready to go, and proper Travis (or whatever) files you want. PkgTemplates.jl works similarly.

You add your code in `mynewpackage/src`.

Whatever packages you use in that code need to be added to the Project.toml. So you `]` to get to the package manager

```julia
dev /paththatdoesnotalreadyexist/mynewpackage
activate mynewpackage
add Statistics LinearAlgebra # and so on

```

It wasn’t always so obvious to me either, but it really is easy.

---

<div class="post-metadata">

### Author: ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)
#### Post date: [April 10, 2020, 9:13pm UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/12 "2020-04-10T21:13:20Z")

</div>

I have been finally packaging my code lately, and once you understand the process and get to know the tools, it isn’t hard. And it is really much better for code reuse and such. My packages are private, not planning on register anything, but I still document everything, write tests, and I’m generally happy with the process. There are a few hitches here and there, for sure, but nothing impossible.

I just finished one package today, separating some code that I had in another package that I finished yesterday.

---

<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: [April 11, 2020, 6:44am UTC](https://discourse.julialang.org/t/proper-way-to-use-modules/28909/13 "2020-04-11T06:44:29Z")

</div>

> [@lewis](#):
>
> making a package is a very sticky wicket indeed

Not really, packages are very lightweight in Julia (like branches in `git` — the idea is that you don’t even think about making one if it is needed), and various tools exist to make creating them convenient, ranging from barebones `pkg> generate` to various template generators.
