# How best to use a custom module?

**URL:** <https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066>\
**Category:** General Usage\
**Tags:** question, modules, code-organization\
**Created:** [February 25, 2022, 1:28pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066 "2022-02-25T13:28:05Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 25, 2022, 1:28pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/1 "2022-02-25T13:28:05Z")

</div>

Hi guys,

I have been away from Julia since end of 2018. Now coming back and looking at my old code now with Julia 1.7, I feel puzzled at how best to use my own modules.

My modules are in subdirectories under the root directory of my project. Like:

./A/ModuleA.jl  
./B/ModuleB.jl

ModuleB uses ModuleA. In my original code of B.jl, I have simply:  
using ModuleA

Then at the REPL started at the root directory, I do:  
include(“B/ModuleB.jl”)

It doesn’t work now as I learned that I need to set the LOAD\_PATH. So I added both A and B onto the LOAD\_PATH, then the above include statement works. But my problem is in VSCode, “goto definition” on a function defined in ModuleA doesn’t work.

Then ModuleB.jl I added:  
include(“…/A/ModuleA.jl”)  
just before  
using ModuleA

Then “goto definition” works in VSCode. But in REPL,  
include(“B/ModuleB.jl”)  
gives my a warning like:  
using A.ModuleA in module ModuleB conflicts with an existing identifier.

So what’s a proper solution to my problem?

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 11:45am UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/2 "2022-02-27T11:45:23Z")

</div>

Is it too difficult a question please?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 27, 2022, 12:44pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/3 "2022-02-27T12:44:34Z")

</div>

The definitions clash because you are using them via the LOAD\_PATH _and_ the include.

Use one or the other

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 1:10pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/4 "2022-02-27T13:10:20Z")

</div>

If I don’t list the subdirectories in LOAD\_PATH and just have include(“…/A/ModuleA.jl”) in ModuleB.jl, then when I do include(“B/ModuleB.jl”) in root directory, I get error:  
ERROR: LoadError: ArgumentError: Package ModuleA not found in current path

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 27, 2022, 1:45pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/6 "2022-02-27T13:45:30Z")

</div>

You either have to do

```julia
julia> push!(LOAD_PATH, "./A")
julia> push!(LOAD_PATH, "./B")
julia> using ModuleA
[Info: Precompiling A [top-level]

julia> using ModuleB
[Info: Precompiling B [top-level]

```

or

```julia
$ mkdir A/src && mv A/ModuleA.jl A/src/
$ mkdir B/src && mv B/ModuleB.jl B/src/ # had a typo (fixed)
$ julia -q

julia> push!(LOAD_PATH, ".")
julia> using ModuleA
[Info: Precompiling ModuleA [top-level]
julia> using ModuleB
[Info: Precompiling ModuleB [top-level]

```

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 1:54pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/7 "2022-02-27T13:54:45Z")

</div>

> [@lawless-m](#):
>
> `mkdir B/src && mv B/ModuleB.jl A/src/`

Is A/src/ supposed to be B/src/ ?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 27, 2022, 1:55pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/8 "2022-02-27T13:55:25Z")

</div>

oh, yes. copy pasting error

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 2:16pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/9 "2022-02-27T14:16:38Z")

</div>

I was actually using your first approach. It works at REPL, but my problem was in VSCode I can’t goto definition in ModuleB.jl. So I added include(“…/A/ModuleA.jl”) there and VSCode works, but then I got the warning of conflict identities.

I tried your second approach with a src subdirectory under each subdirectory of the modules. But it doesn’t seem to work at all. I am not sure how src comes to solve the problem.

With the jl files under its respective src subdirectory, now I start julia in root directory:

$ julia  
\_  
\_ \_ _(_)\_ | Documentation: [https://docs.julialang.org](https://docs.julialang.org)  
(_) | (_) (_) |  
\_ \_ | | \_\_ \_ | Type “?” for help, “]?” for Pkg help.  
| | | | | | |/ ` | |  
| | || | | | (_| | | Version 1.7.2 (2022-02-06)  
_/ |\ __‘_|_|_|\__ ’\_| | Official [https://julialang.org/](https://julialang.org/) release  
|\_\_/ |

julia\> push!(LOAD\_PATH, “.”)  
4-element Vector{String}:  
“@”  
“@v#.#”  
“@stdlib”  
“.”

julia\> using ModuleA  
ERROR: ArgumentError: Package ModuleA not found in current path:

- Run `import Pkg; Pkg.add("ModuleA")` to install the ModuleA package.

Stacktrace:  
[1] require(into::Module, mod::Symbol)  
@ Base ./loading.jl:967

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 27, 2022, 2:25pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/10 "2022-02-27T14:25:54Z")

</div>

OK, this time with includes and testing in VSCode

```julia
#==

matt@fort:~/discourse$ find . | sort

.
./A
./A/A.jl
./B
./B/B.jl
./main.jl

==#

include("A/A.jl")
include("B/B.jl")

using .A 
using .B 

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/514dd01a9ac6227110d381bd7abeb59841791df0.png)

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 5:19pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/11 "2022-02-27T17:19:53Z")

</div>

My issue is since B.jl uses A, in B.jl I need to have:

include(“…/A/A.jl”)  
using A  
…

The above satisfies VSCode for getting to the definition.

But then in REPL when I do:  
using B

Julia complains about the conflicts. Like:  
WARNING: using A.A in module B conflicts with an existing identifier

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 27, 2022, 5:37pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/12 "2022-02-27T17:37:02Z")

</div>

The use of multiple includes is what’s causing the problem.

You could try this where they are all in the same source file

> [@Getting ERROR: LoadError: UndefVarError: Environment not defined when I try to use the modules I created](https://discourse.julialang.org/t/getting-error-loaderror-undefvarerror-environment-not-defined-when-i-try-to-use-the-modules-i-created/75559/2):
>
> Judicious use of import, using and export (the include makes no difference, it is just text subsitution) module Environment export Env mutable struct Env distance\_from\_pasture::Real trips\_milking\_parlor::Integer topography::String end end using .Environment export Env module Cows import ..Env export Cow mutable struct Cow environment::Env end end using .Cows export Cow e = Env(1, 1, "") c = Cow(e)

Or add moduleA to the Manifest of moduleB using Pkg.add(path=“/path/to/A”), create a project in a separate directory to include moduleA and moduleB as dependencies.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 6:51pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/13 "2022-02-27T18:51:25Z")

</div>

getting all my code into one course file is not an option.

Perhaps I will try to put my modules into a package.

I found the core of my issue relates to this unsolved thread:

> [@How to make vscode aware of user modules](https://discourse.julialang.org/t/how-to-make-vscode-aware-of-user-modules/22948):
>
> The vscode plugin is taking the LOAD\_PATH of packages not into account, therefore symbols in my own code are not found. I am trying to fix that, but need some help. What I tried so far: tried to edit the file “~/.vscode/extensions/julialang.language-julia-0.11.5/scripts/languageserver/main.jl”. Adding: push!(LOAD\_PATH, "PATH\_TO\_MY\_MODULES") had now effect. 2. In the following file: [LanguageServer.jl/languageserverinstance.jl at master · julia-vscode/LanguageServer.jl · GitHub](https://github.com/JuliaEditorSupport/LanguageServer.jl/blob/master/src/languageserverinstance.jl) a language s…

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [February 27, 2022, 9:12pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/14 "2022-02-27T21:12:53Z")

</div>

> [@LeoK987](#):
>
> getting all my code into one course file is not an option.

Another option could be to split files and add a third module `C` that contains the code from `A` that uses `B` and the code from `B` that uses `A`. Then put

```julia
using A,B

```

into module `C`. To me this seems less fragile anyway.

> [@LeoK987](#):
>
> Perhaps I will try to put my modules into a package.

I would strongly recommend this. Once you’ve got used to putting things into packages, it seems pretty natural, and in my experience VSCode copes with that as well.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 27, 2022, 9:45pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/15 "2022-02-27T21:45:10Z")

</div>

Just tried to make my modules packages. Basically created one package for one module. It appears much simpler than I imagined.

But I am stuck with a module that has submodules. After the parent module is made a package, it demands about including the submodules as dependences. I know how to add local packages as dependencies with “Pkg\> dev MyPackage”, but that doesn’t do for submodules. What can I do about the submodules?

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [February 27, 2022, 10:07pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/16 "2022-02-27T22:07:40Z")

</div>

It depends how you’re trying to access the other modules in your parent module. For submodules, you usually use `include`, followed by `using`:

```julia
module C
include("/path/to/submoduleA")
include("/path/to/submoduleB")
using .A
using .B
end

```

Note the dots before the module names.  
If `A` and `B` are not submodules, but standalone packages, you need to do either

```julia
Pkg> dev /path/to/package

```

or

```julia
Pkg> add /path/to/package

```

The difference is that `dev` will track changes to the packages, `add` will take a snapshot of the current state and not track changes.  
The choice between submodules or standalone modules depends on your use case.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [February 28, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/17 "2022-02-28T22:20:55Z")

</div>

That works. So I created my packages. Code runs, but I am still having trouble with VSCode not recognizing my own packages.

Say in package C, I have:

```julia
module C
using DataFrames
using MyPackageA
end

```

I have set both DataFrames and MyPackageA as dependencies of C. In VSCode, DataFrames is recognized but MyPackageA is not.

I build my packages with this procedure:

```julia
(v1.7) pkg> generate MyPackage

julia> cd("MyPackage")

pkg> activate .

(MyPackage) pkg> add DataFrames

(MyPackage) pkg> dev ../MyPackage1

```

What is wrong?

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [February 28, 2022, 10:42pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/18 "2022-02-28T22:42:55Z")

</div>

Although I use VSCode occasionally, I’m not very familiar with the details. If I understand [this](https://discourse.julialang.org/t/vscode-intellisense-very-frustrating/72007/3) post correctly, you might need to run in an activated environment that includes your packages as dependencies. You might already be doing that, so you might need to ask another discourse question.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 28, 2022, 10:55pm UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/19 "2022-02-28T22:55:20Z")

</div>

> [@LeoK987](#):
>
> So what’s a proper solution to my problem?

See my answer here: [Best practise: organising code in Julia - #2 by stevengj](https://discourse.julialang.org/t/best-practise-organising-code-in-julia/74362/2)

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [March 1, 2022, 10:34am UTC](https://discourse.julialang.org/t/how-best-to-use-a-custom-module/77066/20 "2022-03-01T10:34:25Z")

</div>

I very agree on this. My other side of the issue beyond code structuring is getting VSCode to function. There seems quite much lack there.
