1

I have the following module:

module TestModule

export foo

function foo()
    return 1
end

end

and a calling script:

using .TestModule

println(foo())

I get the following error:

ERROR: UndefVarError: TestModule not defined

According to the documentation this should work. I know this can be accomplished via include() but I read that this can cause inconsistencies when trying to include the module multiple times. Another way of achieving this seems to be editing the LOAD_PATH but that seems rather inconvenient and also causes the project to not be portable.

I'm wondering if those are the only current solutions to the problem or if there is something better (The two other post regarding this question are 6+ years old and the docs currently state that this is the way to do it, so I wonder if the problem has been fixed by now). Any help is greatly appreciated.

Cheers

  • 3
    Does this answer your question? [How to import custom module in julia](https://stackoverflow.com/questions/37200025/how-to-import-custom-module-in-julia). I guess you're asking a duplicate question. There are various answers there. I wonder if *"The two other post regarding this question are 6+ years old"* would be a good reason to ask the same question repeatedly. – Shayan Nov 17 '22 at 08:58
  • The way to do it then is to add the module to the load path? That means before running someone else's Julia code you have to edit your environment variables? Coming from other languages it seems weird to not be able to split code into modules in a portable way. I thought maybe I overlooked something since the docs don't state that you have to edit the LOAD_PATH. @Shayan – TheLastEngineer Nov 17 '22 at 09:58
  • Pasting the above code into a session, it correctly loads the module, and runs. Which is what the documentation is promising. To first approximation files and modules are completely independent concepts. Messing with LOAD_PATH is a way to muddle this, but (IMO) it sounds like you are looking for the notion of a package, not a module. Packages are a great way to make code portable. – mcabbott Nov 17 '22 at 17:53
  • I see, I did not execute the TestModule file, now it works for this simple example. Thank you. – TheLastEngineer Nov 17 '22 at 18:00
  • `include("./TestModule.jl")` may be helpful – lemmingxuan Nov 18 '22 at 11:25

1 Answers1

0

I'm closing this thread as it has been tagged a duplicate multiple times now. I conclude from this that there currently is no way of splitting code into multiple local modules while maintaining portability. The canonical way of splitting code into modules is by adding the module paths to the LOAD_PATH (julia push!(LOAD_PATH, "\path\to\module")). Thanks to everyone for the feedback nonetheless!

Edit:

After executing the TestModule file it works for this example (I missed that this was necessary, at least in VSCode). My actual program still produces module-not-found errors but since I'm unable to reproduce them in a simple example I'll try figuring it out myself first.

Another Edit:

Renaming the modules solved the problem. I suppose the names were already taken by public packages. For any reader wondering how to solve this problem without renaming the custom module, please have a look at this documentation

Cheers