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:
ModuleB uses ModuleA. In my original code of B.jl, I have simply:
Then at the REPL started at the root directory, I do:
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:
Then “goto definition” works in VSCode. But in REPL,
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:
_ _ ()_ | Documentation: https://docs.julialang.org
() | () () |
_ _ | | __ _ | Type “?” for help, “]?” for Pkg help.
| | | | | | |/ ` | |
| | || | | | (| | | Version 1.7.2 (2022-02-06) / |_’|||_’_| | Official https://julialang.org/ release
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:
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
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.