# Problems about using modules in same file

**URL:** <https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865>\
**Category:** New to Julia\
**Tags:** question, package, modules\
**Created:** [June 13, 2025, 3:23pm UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865 "2025-06-13T15:23:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 13, 2025, 3:23pm UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865/1 "2025-06-13T15:23:40Z")

</div>

I tried code like this

```julia
module moduleA
    function f1()
        println("test")
    end
end

module moduleB
    function f2()
        moduleA.f1()
    end
end

moduleB.f2()

```

but it turned out `ERROR: UndefVarError: `moduleA` not defined`

I found that I can solve this problem via

```julia
module moduleB
    using ..moduleA
    function f2()
        moduleA.f1()
    end
end

```

However, I am not sure about the mechanism behind it, and what if I have many modules like `moduleB` which need to import `moduleA`, is there any convenient way to import the `moduleA` in every `moduleB` ?

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [June 13, 2025, 4:17pm UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865/2 "2025-06-13T16:17:50Z")

</div>

If the inconvenience of merely adding the line `using ..ModuleA` to the code of your other modules seems significant, that would suggest (to me) that they don’t contain enough functionality to warrant the use of separate `module`s and you might be better served by sticking to just a single top-level module.

For a less general answer it might be helpful to share a little more about what your trying to achieve.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 13, 2025, 8:36pm UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865/3 "2025-06-13T20:36:33Z")

</div>

The fundamental purpose of modules is to isolate sets of names from each other like this, in other words modules are namespaces. Imports are how different modules share names with each other (in the case of packages, the first import in a session also irreversibly loads package code, then the following imports just share names again). In a typical `module` block, the only automatic import you have is `using Base`, and you’ll need to do every other import manually. If you don’t want to do that, then you probably don’t want separate modules.

You might still be wondering; why can’t modules just automatically import their own names in each other? After all, the other names in the module are still isolated, so shouldn’t that be close enough? That can’t happen because namespaces also isolate distinct names with the same symbols, so without imports, a `Cells.differentiate` is completely different from `Calculus.differentiate`. To evade the error in your example another way, I can make an entirely separate module also named `moduleA` nested inside `moduleB` to execute different code:

```julia
julia> module moduleB
           module moduleA
               function f1()
                   println("the wrong one")
               end
           end
           function f2()
               moduleA.f1()
           end
       end
Main.moduleB

julia> moduleB.f2()
the wrong one

julia> moduleA.f1()
test

julia> moduleA === moduleB.moduleA
false

```

---

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 14, 2025, 3:27am UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865/4 "2025-06-14T03:27:10Z")

</div>

Thank you very much, it looks very different from Python’s module

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 14, 2025, 4:10am UTC](https://discourse.julialang.org/t/problems-about-using-modules-in-same-file/129865/5 "2025-06-14T04:10:57Z")

</div>

Python modules do namespacing the same way. The only significant differences are the import syntax and that modules are created per file instead of a syntax block. If you had a `moduleA.py` file and a `moduleB.py` file, the latter would need a `from . import moduleA` statement to work too.
