# How can I structure a project in multiple modules and have code run in them?

**URL:** <https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286>\
**Category:** General Usage\
**Tags:** development, module, modules, code-organization, project\
**Created:** [June 13, 2023, 3:16pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286 "2023-06-13T15:16:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [June 13, 2023, 3:16pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/1 "2023-06-13T15:16:00Z")

</div>

Hi all! Sorry this is probably noobish but I couldn’t figure it out easily from the docs/Google.  
I’m developing a small project for my thesis. I generated a project structure (`src/`, `Project.toml` and `Manifest.toml`), and have some code inside `src/`. Until now, I manually loaded and executed code when starting my work session, but now I decided to better organize my code. [I was advised to use modules](https://discourse.julialang.org/t/why-doesnt-this-include-guard-ish-code-evaluate-in-the-correct-order/93108/5), so I `activate`d my project and `add`ed the dependencies, but now I couldn’t understand how to achieve what I need.

For context, the project is called `ARMCortexM4Model`, but the file `ARMCortexM4Model.jl` contains no real code, all the work is done in other files. For instance, I have created a module named `Utils` in `src/Utils.jl` containing some helper functions I need in other scripts.

1. Right now, to use it I need first to

```julia
include("Utils.jl")
using .Utils

```

IIUC I should do something like `] dev Utils` to make Julia aware of the submodule, but I can’t:

```julia
(@v1.9) pkg> activate .
  Activating project at `~/onedrive/tesi/Julia_package/ARMCortexM4Model`

(ARMCortexM4Model) pkg> dev Utils
    Updating registry at `~/.julia/registries/General.toml`
ERROR: The following package names could not be resolved:
 * Utils (not found in project, manifest or registry)
   Suggestions: IOUtils GRUtils MLUtils PkgUtils EHTUtils DCAUtils pciutils_jll

(ARMCortexM4Model) pkg> dev .Utils
ERROR: Unable to parse `.Utils` as a package.

```

My guess is that Julia doesn’t know there are submodules, nor where they are, since they do not appear in the `Project.toml`. **Do I have to edit that? How can I split code in different files otherwise?** (and btw, is this even the idiomatic way to proceed?)

1. I want to use modules also to run some initialization code at start, but just once (a-là `#pragma once` in C/C++). For example, I need to read a large CSV file, and store it in a DataFrame. I so put the loading statement in another module, `Constants.jl`. The idea is to start each “working” script with `using Constants` and have the DataFrame loaded the first time I execute it, but not subsequent times. **Can this be accomplished with modules, maybe, IIUC, using the ` __init__ ` functions?**

Thanks!

---

<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:** [June 13, 2023, 3:21pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/2 "2023-06-13T15:21:19Z")

</div>

Create a file called `src/ARMCortexM4Model.jl` and fill it with

```julia
module ARMCortexM4Model.jl

include("utils.jl")
export Utils

end

```

Assuming your `src/utils.jl` looks like

```julia
module Utils

greet() = println("Hello world!")

end

```

You can then fire up a REPL and write

```julia
julia> using ARMCortexM4Model

julia> Utils.greet()

```

---

<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:** [June 13, 2023, 3:28pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/3 "2023-06-13T15:28:31Z")

</div>

> [@bertulli](#):
>
> I want to use modules also to run some initialization code at start, but just once (a-là `#pragma once` in C/C++). For example, I need to read a large CSV file, and store it in a DataFrame. I so put the loading statement in another module, `Constants.jl`. The idea is to start each “working” script with `using Constants` and have the DataFrame loaded the first time I execute it, but not subsequent times. **Can this be accomplished with modules, maybe, IIUC, using the ` __init__ ` functions?**

I prefer a simple caching mechanism a la

```julia
using DataFrames

const df = DataFrame()

function loaddata(reload=false)
   if reload || isempty(df)
      empty!(df)
      # (re-)load your data into df
   end
   return df
end

```

And then just use the function whenever you need to grab the data.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [June 14, 2023, 9:19am UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/4 "2023-06-14T09:19:15Z")

</div>

> [@fatteneder](#):
>
> You can then fire up a REPL and write
> 
> ```julia
> julia> using ARMCortexM4Model
> 
> julia> Utils.greet()
> 
> ```

Thanks, this partially answers my question. But I wonder:

1. This way I need to load all the submodules at once inside `ARMCortexM4Model`. Can I selectively load just some of them, without modifying `ARMCortexM4Model.jl` every time? In other words, can I just say

```julia
using Utils

```

in a script, and

```julia
using Constants

```

in another one (so to decide every time which subset of helper functions get loaded)?

1. Is there a way to declare more than one module in a single project?

2. I read `] dev` is used to [develop a package](https://pkgdocs.julialang.org/v1/managing-packages/#Adding-a-local-package). But now I don’t understand how: if I am bounded to develop a single package at a time, there’s actually no need to `dev` it in the first place. I thought I could `dev` all the modules I was working on, so that I can independently include them in my working scripts, and use Revise if I needed to modify the modules’ code. What did I understand wrong?

> [@fatteneder](#):
>
> I prefer a simple caching mechanism a la
> 
> ```julia
> using DataFrames
> 
> const df = DataFrame()
> 
> function loaddata(reload=false)
> if reload || isempty(df)
> empty!(df)
> # (re-)load your data into df
> end
> return df
> end
> 
> ```

Thanks, IIUC this also has the advantage of allow the re-loading of the data at your discretion. But just to better understand how modules work, can I do the same using “just” them?

Thanks!

---

<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:** [June 14, 2023, 10:01am UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/5 "2023-06-14T10:01:14Z")

</div>

> [@bertulli](#):
>
> 1. This way I need to load all the submodules at once inside `ARMCortexM4Model`. Can I selectively load just some of them, without modifying `ARMCortexM4Model.jl` every time? In other words, can I just say

Yes. The `export Utils` inside `ARMCortexM4Model.jl` only exports the name `Utils`, but does not export all the names `Utils` it self would export.  
Hence, when you write `using ARMCortexM4Model` you can then use `Utils.greet()` instead of `ARMCortexM4Model.Utils.greet()`. If you also add `export greet` inside `Utils.jl` then you could write

```julia
julia> using ARMCortexM4Model

julia> using Utils

julia> greet()

```

> [@bertulli](#):
>
> 1. Is there a way to declare more than one module in a single project?

Yes and no: A **package** (as generated by running `Pkg.generate("MyPackage")`) defined as a folder with a name `MyProject` containing a `Project.toml` file referring to `MyProject` (and optionally a `Manifest.toml`) and then a `src/MyProject.jl` file. Let’s call this a toplevel package, although it really is an `environment` (or `project`), I think, because it tracks all the dependencies used in it and it happens so that one if it is also called `MyProject`. Think of a `project` like a `virtualenv` in python, if you happen to know that …  
There are then two ways to add more modules:

1. Recommended: Add submodules to `MyProject` as I did it above, which you can optionally re`export`. See also the `@reexport` macro from [`Reexport.jl`](https://github.com/simonster/Reexport.jl).
2. Add another package with `Pkg.generate("MyPackage/Utils")`. You must then tell your toplevel package to `develop` this package, e.g. run `Pkg.develop(path="./Utils")`.

> [@bertulli](#):
>
> 1. I read `] dev` is used to [develop a package](https://pkgdocs.julialang.org/v1/managing-packages/#Adding-a-local-package). But now I don’t understand how: if I am bounded to develop a single package at a time, there’s actually no need to `dev` it in the first place. I thought I could `dev` all the modules I was working on, so that I can independently include them in my working scripts, and use Revise if I needed to modify the modules’ code.

The `dev` is needed so that `Revise.jl` can track its code. The other alternative is to `add` a package, but for that to work the package needs to be `git` repository. The way of adding another module through `dev` is really only meant for local development of packages and its dependencies and/or mono repos like [Makie](https://github.com/MakieOrg/Makie.jl) where they develop multiple packages inside one toplevel package.

In case you haven’t discovered it yet, here is a link to the docs of `Pkg` which talks `packages, environments` a bit more: [1. Introduction · Pkg.jl](https://pkgdocs.julialang.org/v1/)

I also must admit I am not a big fan of this kind of project organization and I feel like I am seeing this question being asked way too often. Perhaps this is an indication that things could be simplified or made more user friendly, but then I did not think how …

> [@bertulli](#):
>
> Thanks, IIUC this also has the advantage of allow the re-loading of the data at your discretion. But just to better understand how modules work, can I do the same using “just” them?

Sorry, didn’t understand the question.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [June 14, 2023, 2:28pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/6 "2023-06-14T14:28:19Z")

</div>

> [@fatteneder](#):
>
> Hence, when you write `using ARMCortexM4Model` you can then use `Utils.greet()` instead of `ARMCortexM4Model.Utils.greet()`. If you also add `export greet` inside `Utils.jl` then you could write
> 
> ```julia
> julia> using ARMCortexM4Model
> 
> julia> using Utils
> 
> julia> greet()
> 
> ```

Thanks, this gets closer to what I want, which by the way is what you explained later 👍 .

> [@fatteneder](#):
>
> In case you haven’t discovered it yet, here is a link to the docs of `Pkg` which talks `packages, environments` a bit more: [1. Introduction · Pkg.jl](https://pkgdocs.julialang.org/v1/)

I read some parts of Pkg’s documentation, but if you are referring specifically to the introduction no, I haven’t, funnily enough. 🙂

> [@fatteneder](#):
>
> > [@bertulli](#):
> >
> > Thanks, IIUC this also has the advantage of allow the re-loading of the data at your discretion. But just to better understand how modules work, can I do the same using “just” them?
> 
> Sorry, didn’t understand the question.

I mean, can modules be used also to initialize some variables/constants one-time-only? Sticking to my case, I need to export a DataFrame variable (easily doable in a module), but it needs to be constructed at runtime, it’s not embedded in the code. If I put this code in a module, and then load it, I should obtain what I want. But I also [read about the ` __init__ ` function](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation). Don’t they do the same thing (initializing things)? If not, what is the difference? And if yes, why do the ` __init__ ` function exist?

Thanks!

---

<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:** [June 14, 2023, 2:46pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/7 "2023-06-14T14:46:54Z")

</div>

> [@bertulli](#):
>
> I mean, can modules be used also to initialize some variables/constants one-time-only? Sticking to my case, I need to export a DataFrame variable (easily doable in a module), but it needs to be constructed at runtime, it’s not embedded in the code. If I put this code in a module, and then load it, I should obtain what I want. But I also [read about the ` __init__ ` function](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation). Don’t they do the same thing (initializing things)? If not, what is the difference? And if yes, why do the ` __init__ ` function exist?

Yes, you could write something like

```julia
module Utils

using DataFrames

const df = DataFrame()

function __init__ ()
   # load something into df
end

end

```

Whenever you then run `using Utils` in a fresh REPL it should load the thing.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 14, 2023, 3:56pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/8 "2023-06-14T15:56:22Z")

</div>

I’m not sure whether there are any unanswered questions left, however, I suggest using the `PkgTemplates` package for generating package templates. It’s used like so, followed by an interactive process:

```julia
using PkgTemplates
generate()

```

Furthermore, if you want an example of a package whose code is tidily structured into modules, you can take a look at my package here: [Neven Sajko / FindMinimaxPolynomial.jl · GitLab](https://gitlab.com/nsajko/FindMinimaxPolynomial.jl)

Also, for your future questions on Discourse, I suggest explaining your problem in more detail, it will allow people to help you better. And it would probably be good to open a separate thread for each separate question.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [June 14, 2023, 6:50pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/10 "2023-06-14T18:50:12Z")

</div>

> [@nsajko](#):
>
> Furthermore, if you want an example of a package whose code is tidily structured into modules, you can take a look at my package here: [Neven Sajko / FindMinimaxPolynomial.jl · GitLab](https://gitlab.com/nsajko/FindMinimaxPolynomial.jl)

Thanks, I see it more or less work as @fatteneder explained. I see that you have at top of `src/Minimax.jl`:

```julia-auto
# Copyright © 2023 Neven Sajko

module Minimax

import
  MathOptInterface,
  Polynomials,
  ..NumericalErrorTypes,
  ..NumericalErrors,
  ..PolynomialPassingThroughIntervals,
  ..LinearInterpolation,
  ..ApproximateInfinityNorm,
  ..IntervalHelpers,
  ..CompressedPolynomials

```

How can you import modules defined in other files, without having `include`d them beforehand?

> [@nsajko](#):
>
> Also, for your future questions on Discourse, I suggest explaining your problem in more detail, it will allow people to help you better. And it would probably be good to open a separate thread for each separate question.

Thanks, I’ll try 🙏 . I put similar questions in a single thread because I thought they were sufficiently tightly-related to be explained in a single context (in other words, I thought having one thread per question would be redundant, since, as the questions were quite close to each other, the same person answering to one would probably have known the answer to the other)

---

<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:** [June 14, 2023, 7:27pm UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/11 "2023-06-14T19:27:13Z")

</div>

> [@bertulli](#):
>
> How can you import modules defined in other files, without having `include`d them beforehand?

The way this works is that it is assumed that the module `Minimax` is `include`d in another module which also contains the modules `NumericalErrorTypes, NumericalErrors, etc.`. This is why those a prefixed with `..` which could be understood as “go up one level and look for `NumericalErrorTypes, ...`”

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 15, 2023, 10:05am UTC](https://discourse.julialang.org/t/how-can-i-structure-a-project-in-multiple-modules-and-have-code-run-in-them/100286/12 "2023-06-15T10:05:51Z")

</div>

> [@bertulli](#):
>
> How can you import modules defined in other files, without having `include`d them beforehand?

Yeah, it’s as fatteneder said, all the `include`s are in the root module, whose file, `FindMinimaxPolynomial.jl`, just looks like this:

```julia
module FindMinimaxPolynomial

include("NumericalErrorTypes.jl")
include("NumericalErrors.jl")
[...]
include("CompressedPolynomials.jl")
include("Minimax.jl")

end

```

The `include`s effectively _include_ all the submodules’ code directly within the `FindMinimaxPolynomial.jl` file.

The submodules then just refer to other modules in the hierarchy.

One awkward thing to keep in mind is that the `include`s have to be ordered according to the module dependency graph. For example, `NumericalErrors` depends on `NumericalErrorTypes`, so `include("NumericalErrorTypes.jl")` has to come before `include("NumericalErrors.jl")`.
