Including a file like this should occur exactly once in only one module. You should not be including it multiple times in multiple modules like this.
I’m confused because you do not actually declare moduleA
or moduleB
anywhere.
Let’s start from scratch.
Let’s construct a proper package.
$ tree
.
├── MyModel
│ ├── Project.toml
│ └── src
│ └── MyModel.jl
└── MyPackage
├── Manifest.toml
├── Project.toml
├── config.toml
└── src
├── ModuleA.jl
├── ModuleB.jl
└── MyPackage.jl
4 directories, 8 files
$ find . -print -exec cat {} \; -printf "\n"
.
cat: .: Is a directory
./MyPackage
cat: ./MyPackage: Is a directory
./MyPackage/Project.toml
name = "MyPackage"
uuid = "331a3bf3-8bf7-4b5c-8fe0-a68622373184"
authors = ["root "]
version = "0.1.0"
[deps]
MyModel = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
./MyPackage/src
cat: ./MyPackage/src: Is a directory
./MyPackage/src/MyPackage.jl
module MyPackage
import TOML
using MyModel: Model, getdata
# I recommend Preferences.jl here
const configs = TOML.parsefile(
joinpath(@__DIR__, "..", "config.toml")
)
const runtimevars = configs["runtimevars"]
const models = Dict{String, Model}()
isentrypoint() = abspath(PROGRAM_FILE) == @__FILE__
for var in runtimevars
models[var] = Model(var)
end
for var in runtimevars
# We might confuse run time and compile time
# by changing model in between includes
# global model = Model(var)
if var == "Evaluate in ModuleA."
include("ModuleA.jl")
elseif var == "Evaluate in ModuleB."
include("ModuleB.jl")
end
end
end # module MyPackage
./MyPackage/src/ModuleA.jl
module A
using ..MyPackage: Model, getdata, isentrypoint, models
processdataA(model::Model) = println(getdata(model))
function __init__()
if isentrypoint()
println(getdata(models["Evaluate in ModuleA."]))
println("We are evaluating in ModuleA.")
end
end
end
./MyPackage/src/ModuleB.jl
module B
using MyModel: Model, getdata
using ..MyPackage: models, isentrypoint
processdataB(model::Model) = println(getdata(model))
function __init__()
if isentrypoint()
processdataB(models["Evaluate in ModuleB."])
println("We are evaluating in ModuleB.")
end
end
end
./MyPackage/Manifest.toml
# This file is machine-generated - editing it directly is not advised
julia_version = "1.9.3"
manifest_format = "2.0"
project_hash = "a583b77fe794703b105dd8a19da10f8b2723c0b5"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.MyModel]]
path = "../MyModel"
uuid = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
version = "0.1.0"
[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.3"
[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
./MyPackage/config.toml
runtimevars=["Evaluate in ModuleA.", "Evaluate in ModuleB."]
./MyModel
cat: ./MyModel: Is a directory
./MyModel/Project.toml
name = "MyModel"
uuid = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
authors = ["root "]
version = "0.1.0"
./MyModel/src
cat: ./MyModel/src: Is a directory
./MyModel/src/MyModel.jl
module MyModel
struct Model
data::String
end
root@localhost:~/hijit# find . -print -exec cat {} \; -printf "\n"
.
cat: .: Is a directory
./MyPackage
cat: ./MyPackage: Is a directory
./MyPackage/Project.toml
name = "MyPackage"
uuid = "331a3bf3-8bf7-4b5c-8fe0-a68622373184"
authors = ["root "]
version = "0.1.0"
[deps]
MyModel = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
./MyPackage/src
cat: ./MyPackage/src: Is a directory
./MyPackage/src/MyPackage.jl
module MyPackage
import TOML
using MyModel: Model, getdata
# I recommend Preferences.jl here
const configs = TOML.parsefile(
joinpath(@__DIR__, "..", "config.toml")
)
const runtimevars = configs["runtimevars"]
const models = Dict{String, Model}()
isentrypoint() = abspath(PROGRAM_FILE) == @__FILE__
for var in runtimevars
models[var] = Model(var)
end
for var in runtimevars
# We might confuse run time and compile time
# by changing model in between includes
# global model = Model(var)
if var == "Evaluate in ModuleA."
include("ModuleA.jl")
elseif var == "Evaluate in ModuleB."
include("ModuleB.jl")
end
end
end # module MyPackage
./MyPackage/src/ModuleA.jl
module A
using ..MyPackage: Model, getdata, isentrypoint, models
processdataA(model::Model) = println(getdata(model))
function __init__()
if isentrypoint()
println(getdata(models["Evaluate in ModuleA."]))
println("We are evaluating in ModuleA.")
end
end
end
./MyPackage/src/ModuleB.jl
module B
using MyModel: Model, getdata
using ..MyPackage: models, isentrypoint
processdataB(model::Model) = println(getdata(model))
function __init__()
if isentrypoint()
processdataB(models["Evaluate in ModuleB."])
println("We are evaluating in ModuleB.")
end
end
end
./MyPackage/Manifest.toml
# This file is machine-generated - editing it directly is not advised
julia_version = "1.9.3"
manifest_format = "2.0"
project_hash = "a583b77fe794703b105dd8a19da10f8b2723c0b5"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.MyModel]]
path = "../MyModel"
uuid = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
version = "0.1.0"
[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.3"
[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
./MyPackage/config.toml
runtimevars=["Evaluate in ModuleA.", "Evaluate in ModuleB."]
./MyModel
cat: ./MyModel: Is a directory
./MyModel/Project.toml
name = "MyModel"
uuid = "99c7e603-0d09-40a3-8af7-b79e4bce3e38"
authors = ["root "]
version = "0.1.0"
./MyModel/src
cat: ./MyModel/src: Is a directory
./MyModel/src/MyModel.jl
module MyModel
struct Model
data::String
end
getdata(model::Model) = model.data
end # module MyModel
Executing…
$ julia --project=MyPackage MyPackage/src/MyPackage.jl
Evaluate in ModuleA.
We are evaluating in ModuleA.
Evaluate in ModuleB.
We are evaluating in ModuleB.
Loading as a library:
$ cd MyPackage && julia --project -e "using MyPackage"
# no output
Having formatting issues. Will fix later. Scroll right
Interactive usage:
julia> using Pkg
julia> pkg"activate MyPackage" Activating project at `~/hijit/MyPackage`
julia> using MyPackage
julia> model = MyPackage.models["Evaluate in ModuleA."]
MyModel.Model("Evaluate in ModuleA.")
julia> MyPackage.A.processdataA(model)
Evaluate in ModuleA.