How to use local, relative modules (best practice, simple)?

jakob1
Wed, 25 May 2022 06:31:14 +0000
New to Julia

Hi,

Julia states to feel like a scripting language. Hence I would like to find out the most simple way to modularize (smaller) projects in julia.

Here is a minimal example that worked for me after some research and trial & error -
Main script main.jl using helper module helper.jl.

Directory structure:

testproj
  - helper.jl
  - main.jl

helper.jl:

module Helper

function helloworld()
    println("Hello world")
end

end

main.jl:

include("./helper.jl")
using .Helper: helloworld

helloworld()

include here is suboptimal, if I have read correctly:

The expression include("source.jl") causes the contents of the file source.jl to be evaluated in the global scope of the module where the include call occurs. If include("source.jl") is called multiple times, source.jl is evaluated multiple times.

Also we need two lines of code for module import instead of just one - include and using.

1. Is there any way to reduce module imports to one line - using .Hello: helloworld?

2. In this context: is modifying LOAD_PATH to include the current project a viable thing? How and where would I want to modify this path locally inside my project?

3. Is there any way to “auto-wrap” a file as module, so we don’t need module Helper... end inside helper.jl, as used in other languages like Python, JavaScript?

Thanks in advance!
jakob

16 posts - 11 participants