# How to Import dependency from one file to another inside of a package?

**URL:** <https://discourse.julialang.org/t/how-to-import-dependency-from-one-file-to-another-inside-of-a-package/42891>\
**Category:** New to Julia\
**Tags:** package, modules, code-organization\
**Created:** [July 11, 2020, 10:28am UTC](https://discourse.julialang.org/t/how-to-import-dependency-from-one-file-to-another-inside-of-a-package/42891 "2020-07-11T10:28:31Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [July 11, 2020, 11:52am UTC](https://discourse.julialang.org/t/how-to-import-dependency-from-one-file-to-another-inside-of-a-package/42891/5 "2020-07-11T11:52:35Z")

</div>

I had the same questions when I developed my own package.

The most important things to retain are:

- the logical structure of your program can be decoupled from the actual division in files (i.e. no reason to have necessarily one module == one file like in Python)
- you can have one top module and several submodules if you wish, although this is in general discouraged
- there is no issues in calling `import x` or `using x` several times in the code, BUT `include("x.jl")` should be called only once. `include` is like copying everything from the included file in the point where it has been included.
- you can use the [reexport package](https://github.com/simonster/Reexport.jl) to automatically reexport the functions/objects you imported from an other module

In my case I have:

**[BetaMl.jl](https://github.com/sylvaticus/BetaML.jl/blob/master/src/BetaML.jl) file:**

```julia
module BetaML
include("Utils.jl")
using .Utils
include("Nn.jl")
using .Nn
include("Perceptron.jl")
using .Perceptron
include("Clustering.jl")
using .Clustering
end # module

```

**Nn.jl file:** (similar for the other files)

```julia
module Nn
using [list of packages I need for Nn functionality]
@reexport using ..Utils # note the two dots in front to go one level up, as these two modules are siblings
export [list of Nn module provided functions]
[...code...]
include("Nn_default_layers.jl")
[...code...]
include("Nn_default_optalgs.jl")
[...code...]
end # end module

```

In the “Nn\_default\_layers.jl” and “Nn\_default\_optalgs.jl” file I then have:

```julia
using [list of packages I need in this "section" of the Nn module]
export [list of functions/objs provided in this "section" of the Nn module]
[...code...]

```

An user of the package can then access its functionality either by `using` the specific submodule of interest and then directly the provided functionality or by `using` the root module `BetaML` and then prefix with `BetaML.` each object/function she/he want to use, e.g.:

```julia
using BetaML.Nn
myLayer = DenseLayer(2,3)

```

or, equivalently,

```julia
using BetaML
res = BetaML.kernelPerceptron([1.1 2.1; 5.3 4.2; 1.8 1.7], [-1,1,-1])

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-import-dependency-from-one-file-to-another-inside-of-a-package/42891)._
