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.
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
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
() | () () |
_ _ | | __ _ | Type “?” for help, “]?” for Pkg help.
| | | | | | |/ ` | |
| | || | | | (| | | Version 1.7.2 (2022-02-06) / |_‘|||_’_| | Official https://julialang.org/ release
|__/ |
The use of multiple includes is what’s causing the problem.
You could try this where they are all in the same source file
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.
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
using A,B
into module C. To me this seems less fragile anyway.
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.
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?
It depends how you’re trying to access the other modules in your parent module. For submodules, you usually use include, followed by using:
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
Pkg> dev /path/to/package
or
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.
Although I use VSCode occasionally, I’m not very familiar with the details. If I understand this 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.