# How to add/create/use submodules?

**URL:** <https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329>\
**Category:** New to Julia\
**Created:** [August 29, 2023, 12:55pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329 "2023-08-29T12:55:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [August 29, 2023, 12:55pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/1 "2023-08-29T12:55:20Z")

</div>

In the [documentation for modules](https://docs.julialang.org/en/v1/manual/modules/), I understand that the _name_ of a module (or submodule) does not necessarily have to be the same as the module name - since the module name is a namespace. I’m having trouble loading a submodule - but I’m also sure I’m missing something important.

Once I figure everything out, I will start with [PkgTemplates](https://github.com/JuliaCI/PkgTemplates.jl), buf for simplicity here I created an example project with `] generate Stuff`, I then created a file in `Stuff/src`: `crc.jl`.

> **The file contains code like:**
>
> ```julia
> module CRC
> export crc
> 
> table::Vector{UInt32} = Vector{UInt32}(undef, 256)
> 
> function compute_table()
> # code elided
> end
> 
> function __init__ ()
> compute_table()
> end
> 
> function crc(buffer)
> # code elided
> end
> 
> end # module CRC
> 
> ```

I’m aware that there are several very mature projects that compute CRCs, this is just something that was handy to use as an example. I was hoping, in `Stuff/src/Stuff.jl`, to be able to use the `crc` function that was exported from the submodule by issuing `using .CRC` … but the local module is not found.

Did I need to add my code to the project with `] dev` or something?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [August 29, 2023, 1:19pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/2 "2023-08-29T13:19:35Z")

</div>

EDIT: Thanks to @disberd for pointing out that one has to use `using .CRC` in this case.

```julia
# src/Stuff.jl
module Stuff

include("crc.jl")
using .CRC

end

```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [August 29, 2023, 1:31pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/3 "2023-08-29T13:31:26Z")

</div>

```julia
ERROR: LoadError: ArgumentError: Package Stuff does not have CRC in its dependencies:

```

```julia
] add .CRC
ERROR: Unable to parse `.CRC` as a package.

```

I picked the submodule name CRC on purpose - because it collided with a package that was in the global repository, so that I could better understand how modules worked …

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 29, 2023, 1:35pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/4 "2023-08-29T13:35:53Z")

</div>

I think here you’d have to write

```julia
using .CRC

```

as without the `.` it will look for a package named CRC inside the package environment.  
WIth the `.` you are specifying to reference to the locally defined submodule CRC.

For submodules that are not packages themselves (but are simply defined in the parent module namespace like with `include("crc.jl")` above) you don’t have to use `]add`

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [August 29, 2023, 1:37pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/5 "2023-08-29T13:37:18Z")

</div>

Here is a simplified example. The example package has a file structure like:

```julia
.
├── Project.toml
└── src
   ├── crc.jl
   └── Stuff.jl

```

In `crc.jl`:

```julia
module CRC
export crc

crc(buffer) = 0x01020304

end # module CRC

```

And in `Stuff.jl`:

```julia
module Stuff

include("crc.jl")
using .CRC # note the leading dot!

end # module Stuff

```

Now, since the `CRC` module exported `crc` from `CRC` to `Stuff`, we can do:

```julia
julia> using Stuff

julia> Stuff.crc
crc (generic function with 1 method)

```

To also have `crc` exported from `Stuff`, you’d need another `export` statement in `Stuff.jl`

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [August 29, 2023, 1:40pm UTC](https://discourse.julialang.org/t/how-to-add-create-use-submodules/103329/6 "2023-08-29T13:40:59Z")

</div>

Indeed, you are right, my bad. I update my answer!
