# Proper way of organizing code into subpackages

**URL:** <https://discourse.julialang.org/t/proper-way-of-organizing-code-into-subpackages/52835>\
**Category:** New to Julia\
**Tags:** packages, code-organization\
**Created:** [January 4, 2021, 5:34pm UTC](https://discourse.julialang.org/t/proper-way-of-organizing-code-into-subpackages/52835 "2021-01-04T17:34:30Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [January 4, 2021, 6:46pm UTC](https://discourse.julialang.org/t/proper-way-of-organizing-code-into-subpackages/52835/4 "2021-01-04T18:46:19Z")

</div>

### Tips directory structure/`LOAD_PATH`

If you are starting such a large project (approx 20 modules), I suggest taking advantage of `LOAD_PATH` instead of using `pkg> add` or `pkg> dev`. It is the easiest way to learning Julia development, and you can migrate to the `pkg>` system when your are closer to publishing your solution to Julia’s `General` registry (if you so desire).

### Quick dev: 1 file per “software module”:

If you are migrating from Python, you might want to try the 1 file per “software module” solution:

```julia-auto
my_package_repo
├── Module1.jl
├── Module2.jl
├── Module3.jl
...
├── ModuleN.jl

```

NOTE:

- I myself have never tried this “single file” module solution, but it is supposed to work.
- Don’t forget that the code from each Modulei.jl file must be encased in a `module Modulei` … `end` block.

### Add `my_package_repo` to `LOAD_PATH`

You just have to make sure you first add:

```julia-auto
push!(LOAD_PATH, "/path/to/my_package_repo")

```

You can set this variable in your `~/.julia/config/startup.jl` or set it from a shell environment variable:

- [Environment-Variables](https://docs.julialang.org/en/v1/manual/environment-variables/#Environment-Variables)
- [JULIA\_LOAD\_PATH](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_LOAD_PATH)

If you do this, you should _ **no longer use** _ `pkg> add Module1`. `LOAD_PATH` takes care of making it available to your project.

### Typical package directory structure:

But to be able to migrate to the `pkg>` system more smoothly, I suggest you use the proper Julia directory structure from the start. Among other benefits, this structure (optionally) includes `test/` folders for ci tests, and more clearly groups together package solutions that are split across multiple files.

```julia-auto
my_package_repo
├── Module1
│ ├── Project.toml (optional)
│ ├── src
│ │ ├── Module1.jl
│ │ ├── Module1SubFile1.jl
│ │ └── Module1SubFile2.jl
│ └── test
│ └── runtests.jl
├── Module2
│ └src
│ └── Module2.jl
├── Module3
│ └src
│ └── Module3.jl
...
└── ModuleN
    └src
        └── ModuleN.jl

```

Again, I re-iterate:

- Don’t forget that the code from each Modulei.jl file must be encased in a `module Modulei` … `end` block.

---

_[View the full topic](https://discourse.julialang.org/t/proper-way-of-organizing-code-into-subpackages/52835)._
