In a project directory,
I have a main.jl and a myModule.jl
myModule.jl. contains
module myModule
…
end
In the main.jl,
how do I use myModule ?
using myModule
and
import myModule
both are for modules already registered by Pkg.
thanks
HP
In a project directory,
I have a main.jl and a myModule.jl
myModule.jl. contains
module myModule
…
end
In the main.jl,
how do I use myModule ?
using myModule
and
import myModule
both are for modules already registered by Pkg.
thanks
HP
The simplest thing is:
julia> push!(LOAD_PATH, ".")
4-element Array{String,1}:
"@"
"@v#.#"
"@stdlib"
"."
julia> using myModule
By pushing to LOAD_PATH, you tell Julia to also look in the current folder (“.”) when loading modules.
For a slightly more advanced version, try this:
]generate myModule (pressing the ] key to get the pkg> prompt in Julia)
src folder and a new src/myModule.jl fileProject.toml in your current foldersrc/myModule.jl
julia --project
Project.toml that you just generatedYou should then be able to do using myModule without any modification of LOAD_PATH, as long as you start julia with julia --project
The advantage of this approach is that you now have a brand new clean environment for adding any new packages that you might need for working on myModule, and you can install packages into that environment without messing up the rest of your system.
A third option is
include("myModule.jl")
using .myModule # <-- the . is important!
In case an explanation helps someone:
using .myModule
is AFAIK equivalent to
using CurrentModule.myModule.
I want to emphasize this more. You should always use ] generate to use a local module. Just creating the right folder structure won’t make you able to do using when it is local.