suiato
February 29, 2024, 4:16am
1
I have a simple question: what is import <module>
supposed to do?
Say, a module A.jl is
module A
export sayA
function sayA()
println("This is A.")
end
end
and, test.jl is
import A
sayA()
They are in the same directory, and the directory is added to LOAD_PATH via startup.jl. Running include("test.jl")
in a terminal results in
This is A.
Is this expected?
I thought I had to do import A : sayA
in test.jl.
If I replace import A
with import A : sayA
in test.jl, I get the error:
ERROR: LoadError: MethodError: no method matching (::Colon)(::Nothing, ::typeof(sayA))
...
When I replace import
with using
, the results are the same. Then, no difference between import
and using
Is this normal, or is something wrong with my understanding or my Julia environment? I use Julia Version 1.10.1 (2024-02-13) and VScode on Windows 11.
1 Like
mkitti
February 29, 2024, 7:13am
2
suiato:
include(âtest.jlâ)
I cannot reproduce. I get
julia> include("test.jl")
ERROR" LoadError: UndefVarError: `sayA` not defined
There should not be a space between the A and colon.
julia> import A : sayA
ERORR: UndefVarError: `sayA` not defined
julia> import A: sayA
What is in your startup.jl ? Try starting Julia with --startup-file=no
.
3 Likes
suiato
February 29, 2024, 5:09pm
3
Thank you, mkitti.
I tried julia --startup-file=no
, but I got
julia> julia --startup-file=no
ERROR: ParseError:
# Error @ REPL[2]:1:7
julia --startup-file=no
# ââ ââ invalid operator
Stacktrace:
[1] top-level scope
@ none:1
My startup.jl
file is like
using Revise
Revise.includet("C:/...Config.jl")
Revise.includet("C:/...Types.jl")
push!(LOAD_PATH, "C:/...Projects/mlp/src")
There should not be a space between the A and colon.
Oh, that made it work! Thanks
It resolves the error part of my question.
I still wonder why
import A
sayA()
in test.jl works in my case, while it doesnât in your case. I understand it shouldnât.
suiato
February 29, 2024, 6:17pm
4
The remaining problem is resolved:
I commented out the lines in startup.jl except the line for push!(LOAD_PATH,...)
, created C.jl, modified test.jl to import the C module, and started a new Julia session. When I ran tests.jl, it failed correctly
It seems starting a new Julia session was the answer. I put the lines I commented out in the startup.jl back, and test.jl ran correctly in that situation.