`import <module>` makes all objects available without prefix

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.")

and, test.jl is

import A

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 :open_mouth:

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

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.


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
 [1] top-level scope
   @ none:1

My startup.jl file is like

using Revise
push!(LOAD_PATH, "C:/...Projects/mlp/src")

There should not be a space between the A and colon.

Oh, that made it work! Thanks :slight_smile:
It resolves the error part of my question.

I still wonder why

import A

in test.jl works in my case, while it doesn’t in your case. I understand it shouldn’t.

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 :slight_smile:

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.