# Importing a package to all the submodules of a module

**URL:** <https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148>\
**Category:** General Usage\
**Tags:** type, module\
**Created:** [November 18, 2017, 1:33pm UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148 "2017-11-18T13:33:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [November 18, 2017, 1:33pm UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/1 "2017-11-18T13:33:04Z")

</div>

I want to import a common package to all the submodules inside my module. How to do this?

Example

```julia
module X1
    using Distributions
    abstract type abstract_Y end
    # How to make both 'abstract_Y' and 'Distributions' available to all 
    # the submodules of this module without explicitly mentioning it in
    # submodules?
    module Y1
        using ..abstract_Y # How to avoid this??
        using Distributions # And this too??
        type Type1 <: abstract_Y
        end
        function func1( x::Type1 ) 
            # Do something with x of Y1.Type1
            rand( Bernoulli(0.9) )
        end
    end
    module Y2
        using ..abstract_Y
        using Distributions
        type Type1 <: abstract_Y # I need to reuse name 'Type1' in this module
        end
        function func1( x::Type1 )
            # Do something with x of Y2.Type1
            rand( Normal(2.0,1.0) )
        end
    end
    # ...
    # module Yn
    # ....
    # end
end

```

Here X1 is my parent module, and Y1 and Y2 are children. All my submodules will have definition for a type ‘Type1’ and assocsiated functions. That’s why I want to make them into multiple modules.

With Thanks,  
Vishnu Raj

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 18, 2017, 5:33pm UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/2 "2017-11-18T17:33:47Z")

</div>

Why do you want to use the same type name in the submodules? It doesn’t seem like a good idea to use the same name for two different things.

Or what are you trying to do?

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [November 19, 2017, 6:22am UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/3 "2017-11-19T06:22:03Z")

</div>

I’m a PhD scholar is working on a package to reproduce the results in few other papers.

I’m planning to write one module for each of the papers I want to reproduce the results and inside that I can put the methods to reproduce each of the figures.

Say I want to reproduce Fig.1 and Fig. 2 from Paper A and Fig.1, 2, 3 from Paper B. I’m planning to write it as

```julia

module ReproducibleResearch
    using Distributions
    abstract type BaseExperiment end
    
    # Module for Paper A
    module PaperA
        using ..BaseExperiment
        using Distributions # All my papers depend on 'Distributions' package
        # but how to make them available to submodules by default
        # For Figure 1
        type Fig1 <: BaseExperiment
            # Specifics for this figure
        end
        function run( x::Fig1 )
            # Do experiment and plot the results
        end
        # For Figure 2
        type Fig2 <: BaseExperiment
            # Specifics for this figure
        end
        function run( x::Fig2 )
            # Do experiment and plot the results
        end
    end

    # Module for Paper B
    module PaperB
        using ..BaseExperiment
        using Distributions # All my papers depend on 'Distributions' package, 
        type Fig1 <: BaseExperiment
            # Specifics for this figure
        end
        function run( x::Fig1 )
            # Do experiment and plot the results
        end
        
        type Fig2 <: BaseExperiment
             # Specifics for this figure
        end
        function run( x::Fig2 )
            # Do experiment and plot the results
        end

       type Fig3 <: BaseExperiment
             # Specifics for this figure
        end
        function run( x::Fig3 )
            # Do experiment and plot the results
        end
    end
end

```

Now you see why I need to reuse the Type names.  
When I want to run, I can do

```julia
julia> x1 = Paper1.Fig1( args )
julia> x2 = Paper2.Fig1( args )
julia> p1 = run( x1 ) # This will return handles for plots of the results
julia> p2 = run( x2 ) # Again, handle for plots the results

```

This is the reason why I need to reuse type names.

This is one method I thought out to solve my problem.  
Is there any other solutions to solve this reusing `FigX` problem?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 19, 2017, 7:49am UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/4 "2017-11-19T07:49:14Z")

</div>

Perhaps I am missing something about the setup, but the way I would do this is

1. factor shared code and dependencies into a single module

2. make other packages use this.

I would avoid making any submodules at all. The cost is this is a `using CommonModule` in each module. The benefit is a much more modular setup.

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [November 20, 2017, 4:57am UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/5 "2017-11-20T04:57:48Z")

</div>

Thanks @Tamas_Papp.  
But here, my submodules have different codes. I just need few packages to be automatically available for the sub-module (instead of `using Distributions` inside every submodule).

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 20, 2017, 10:50am UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/6 "2017-11-20T10:50:08Z")

</div>

As far as I know, the answer is: you can’t. Modules in Julia (unlike e.g. Python) intentionally do not contain symbols from an outer module that contains them. That’s inconvenient in your current case but has the nice property of making module namespaces easier to reason about.

If you want, you could write a macro that inserts the appropriate set of `using` declarations. But the standard syntax of `using Foo, Bar` is already pretty terse. How many packages do you actually want to be using inside each submodule?

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [November 20, 2017, 1:45pm UTC](https://discourse.julialang.org/t/importing-a-package-to-all-the-submodules-of-a-module/7148/7 "2017-11-20T13:45:10Z")

</div>

Thanks @rdeits.  
My submodules are also split into multiple files and I have to use 5-6 packages inside each submodule module. I put those common packages inside a `common.jl` file and uses `include("common.jl")` inside each of the submodule.
