# Best practice for managing package hierarchy

**URL:** <https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822>\
**Category:** New to Julia\
**Tags:** package, module\
**Created:** [February 23, 2021, 5:23am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822 "2021-02-23T05:23:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [February 23, 2021, 5:23am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/1 "2021-02-23T05:23:18Z")

</div>

What’s the best practice for the management of a package and its modules?  
Please take a look at the following example.  
In this example, module `A` is a kind of a Base module, and module `B` is a corresponding module.  
Also, I’d like to make functions accessible as `MyPackage.my_func` (without exporting `my_func` like using `Reexport`).

What should I do?

- `src/MyPackage.jl`

```julia
module MyPackage
    # `include("A.jl"); using .A` does not make support `MyPackage.A`.
end

```

- `src/A.jl`

```julia
module A
    abstract type AbstractA end
    function my_func(a::AbstractA)
        # do something
    end
end

```

- `src/B.jl`

```julia
module B
    # how to use AbstractA here?
end
```

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [February 23, 2021, 5:40am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/2 "2021-02-23T05:40:44Z")

</div>

Now I’m taking the following way but I think it’s too confusing.

- `src/MyPackage.jl`

```julia
module MyPackage
    export AbstractA
    abstract type AbstractA end
    include("A.jl")
    @reexport using .A
    using .A: my_func # to make not exported functions accessible as `MyPackage.my_func`
    include("B.jl")
    @reexport using .B
end

```

- `src/A.jl`

```julia
module A
    using MyPackage: AbstractA
    # ...
end

```

- `src/B.jl`

```julia
module B
    using MyPackage: AbstractA
    # ...
end

```

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [February 23, 2021, 6:44am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/3 "2021-02-23T06:44:15Z")

</div>

Why would you constrain your namespace so much? I mostly have **one** `module` where I do `include` various `*.jl` files. This is sufficient even for medium size libraries.

One alternative is to organize your module into submodules as described in [Submodules and relative paths](https://docs.julialang.org/en/v1.6-dev/manual/modules/#Submodules-and-relative-paths) in the Julia docs. That could give you a cleaner and easier organization.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [February 23, 2021, 7:20am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/4 "2021-02-23T07:20:49Z")

</div>

Sorry but I didn’t understand.  
Probably I’ve not figured out how to manage Julia packages.

If you don’t mind, can you give me an example in detail?  
It would be much better if you apply your way to the example I wrote.

+) I prefer to manage a package by writing files for each module.  
Your suggestion may be writing a whole library within a file and employing submodule-relative path methodology. Right?

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [February 23, 2021, 7:53am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/5 "2021-02-23T07:53:19Z")

</div>

ok, like you I would have

- `src/MyPackage.jl`, then

```julia
module MyPackage

abstract type AbstractA end

include("A.jl")
include("B.jl")
...

export AbstractA, my_func, my_other_func

end

```

- `src/A.jl` does not need to be another module, just gets included into `MyPackage`. Since` AbstractA` was declared **before** `A.jl` got included, you can use it:

```julia
# src/A.jl
function my_func(a::AbstractA) 
    # do something
end

```

- the same goes for `src/B.jl` (you don’t need another module):

```julia
# src/B.jl
function my_other_func(a::AbstractA)
    # do something else
end

```

If you write `include` above, the compiler sees `MyPackage.jl` like one larger source file, but you have the convenience to organise your functionality into several files. Just take care that types like your `AbstractA` are declared before being used in functions or other type declarations.

I usually have one source file like `types.jl` and `include` it first, where I declare common types, used by other source files `include`d after that. Thus I do not need to declare types in my main module file like `MyPackage.jl`.

Does that make more sense now for you?

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [February 23, 2021, 7:56am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/6 "2021-02-23T07:56:13Z")

</div>

Oh, I didn’t know that declaring `AbstractA` before `include("A.jl")` makes it possible to use the type in `A.jl`.  
It’s wonderful! Exactly I’ve found it and spent long time to understand the management of Julia package.

Also, I’ve taken a look at packages you’ve written. It makes everything clear. Thanks a lot @pbayer !

EDIT: Then, it would be very important to place `include("some_files.jl")` in an appropriate order.  
EDIT2: For other readers who are confused like me, I would say that `include("my_file.jl")` will work as if the code written in `my_file.jl` is at the place where the `include` is.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [February 23, 2021, 8:18am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/7 "2021-02-23T08:18:32Z")

</div>

Glad to help!

I also learned a lot about code organization by looking at other libraries. For example I can recommend very much to look at:

[https://github.com/JuliaDiff/TaylorSeries.jl](https://github.com/JuliaDiff/TaylorSeries.jl)

The Julia source code itself is so huge, it can be overwhelming at first.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 23, 2021, 10:38am UTC](https://discourse.julialang.org/t/best-practice-for-managing-package-hierarchy/55822/8 "2021-02-23T10:38:02Z")

</div>

> [@iHany](#):
>
> EDIT2: For other readers who are confused like me, I would say that `include("my_file.jl")` will work as if the code written in `my_file.jl` is at the place where the `include` is.

hmmmmm… almost. The included file is evaluated at the global scope of the module calling it. So, if the code included declares a variable `myVar`, and you call `include(....)` within for example a `for` loop or a function, `myVar` would be in the global scope of the module, not in the local scope of the `for` block or of the function.
