# Submodules and variable scopes

**URL:** <https://discourse.julialang.org/t/submodules-and-variable-scopes/68161>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 14, 2021, 6:24pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161 "2021-09-14T18:24:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 6:24pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/1 "2021-09-14T18:24:23Z")

</div>

I have a quick question about sub modules and scope. I have tried to search for similar questions but have not found an answer, so sorry if this has been asked before:

I have created a project directory with the following modules

```julia
module Calculator
  
export interest, rate, plusone

include("Plus_one.jl")

"""
    interest(amount, rate)

Calculate interest from an `amount` and interest rate of `rate`.
"""
function interest(amount, rate)
    return amount * (1 + rate)
end

"""
    rate(amount, interest)

Calculate interest rate based on an `amount` and `interest`.
"""
function rate(amount, interest)
    return interest / amount
end

end # module
~               

```

and the Plus\_one.jl is here:

```julia
#Plus_one.jl
module Plus_one

export plusone

plusone(x::Int) = x +1

end #module
~                                                                                                                                                                                                                               

```

From my understanding, include(“Plus\_one.jl”) is the equivalent of copy and pasting that code into my Calculator.jl. I export plusone from module Plus\_one into the Calculator scope, and then I export it again from there into the scope of whatever uses Calculator.

However, when I start a Julia Project in this directory and “use Calculator” I get unexpected behavior. interest and rate are exported as expected. If I type either “interest” or “rate” into the repl it recognizes it as a function. However if I type plusone into the repl I get an error. It recognizes Calculator.Plus\_one.plusone, however, I don’t understand why plusone is not getting exported into the global namespace?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 14, 2021, 6:30pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/2 "2021-09-14T18:30:27Z")

</div>

You need `using .Plus_one` after you do the `include` bit, I think.

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 6:38pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/3 "2021-09-14T18:38:07Z")

</div>

Ok that makes sense. So export statements aren’t “triggered” unless you are using that module

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 6:41pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/4 "2021-09-14T18:41:16Z")

</div>

However, this brings me to a new issue. If I add that “using” statement Julia informs me that Calculator does not have Plus\_one listed as a dependency in the manifests. How can I add a local file to the dependency list? I have my project set to the current working directory, I go into the package manager and type “add path/to/Plus\_one.jl” but that doesn’t seem to work!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 14, 2021, 6:44pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/5 "2021-09-14T18:44:56Z")

</div>

I can’t replicate your error (just set up my own project and used a sub-module). Do you have the `.` in there? It needs to be `.Plus_one`, not just `Plus_one`.

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 6:52pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/6 "2021-09-14T18:52:25Z")

</div>

Are you saying that it should be `include("./Plus_one.jl")` ?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 14, 2021, 6:54pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/7 "2021-09-14T18:54:08Z")

</div>

No. Here is the syntax that works for me

```julia
module MyPkg

export bb

greet() = print("Hello World!")

include("submod.jl")

using .SubMod

end # module

```

with `submod` being

```julia
module SubMod

export bb

bb = 1

end

```

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 7:19pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/8 "2021-09-14T19:19:54Z")

</div>

Ok I got it working. Why is the `.` necessary here? Is it because Submod or in my case Plus\_one.jl is not a package, so this lets the repl know it’s just a general script?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 14, 2021, 7:21pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/9 "2021-09-14T19:21:58Z")

</div>

Yes. It’s to resolve the exact ambiguity that was causing you problems. The `.` means that it’s a module that was defined inside the current module, more or less. It’s a bit of a unix-ism, for sure.

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [September 14, 2021, 7:31pm UTC](https://discourse.julialang.org/t/submodules-and-variable-scopes/68161/10 "2021-09-14T19:31:04Z")

</div>

Makes perfect sense, thanks!
