# Does Julia allow subpackages?

**URL:** <https://discourse.julialang.org/t/does-julia-allow-subpackages/6207>\
**Category:** General Usage\
**Created:** [October 2, 2017, 8:57pm UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207 "2017-10-02T20:57:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 2, 2017, 8:57pm UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/1 "2017-10-02T20:57:07Z")

</div>

Is it possible to define a module A and a submodule B without including B code in A directly or via an include() call? The goal is for “import A” not to load A.B and require users to explicitly “import A.B”.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 2, 2017, 10:27pm UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/2 "2017-10-02T22:27:13Z")

</div>

Well, if you don’t export `B` from the `A` module you need to explicitly `import A.B` to use the `B` module:

```julia
julia> module A
       module B
       export foo
       foo() = 42
       end
       end
A

julia> import A

julia> B.foo() # errors cause we have not imported A.B
ERROR: UndefVarError: B not defined

julia> import A.B

julia> B.foo()
42

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 3, 2017, 1:10am UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/3 "2017-10-03T01:10:03Z")

</div>

> [@fredrikekre](#):
>
> Well, if you don’t export B from the A module you need to explicitly import A.B to use the B module:

This is not quite what I want. I want to have module B code in a separate file which would only be loaded when I do import A.B, but not when I do import A. This is how modules defined inside a package behave in Python.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 3, 2017, 3:57am UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/4 "2017-10-03T03:57:19Z")

</div>

See my latest post in [https://github.com/JuliaLang/julia/issues/4600](https://github.com/JuliaLang/julia/issues/4600) for a plan to implement what I think you’re asking about.

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 3, 2017, 4:23am UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/5 "2017-10-03T04:23:22Z")

</div>

@StefanKarpinski - your post seems to be about relative imports. What I am looking for is a much simpler scheme. I expected that `import A.B` would first `include("src/A.jl/A.jl")`. If doing that creates `A.B` module, we are done. If not, look at `src/A.jl/B.jl`. If that is a file - do `include("src/A.jl/B.jl")` in the module A context. If it is a directory, do the same with `src/A.jl/B.jl/B.jl`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 3, 2017, 5:20pm UTC](https://discourse.julialang.org/t/does-julia-allow-subpackages/6207/6 "2017-10-03T17:20:49Z")

</div>

The [proposal](https://github.com/JuliaLang/julia/issues/4600#issuecomment-327573232) that I was referring to (“my latest post”) addresses exactly that. It works for relative or absolute imports as long they are for not-yet-defined submodules of some module that you are currently loading, which seems to be just what you’re asking about.
