How to properly bring in a module and use its namespace

Hello, Julia noob here. I am trying to take some code that worked in a previous version of Julia (0.6 I think?) and get it working in the current version (1.6.2). The problem I am having is in the previous version, they did the following to get access to a local module:

push!(LOAD_PATH, “./”)
using myModule

And then used the exports from myModule as is without having to specify namespace. So if myModule has

export x
x = 1

Then the code could use x without having to say myModule.x.
However, in 1.6, this import sequence does not work the same.

It says

ERROR: LoadError: ArgumentError: Package myModule not found in current path

after the using line

If I do

push!(LOAD_PATH, “./”)
import myModule

instead, I get the same error.

I can do

push!(LOAD_PATH, “./”)

but then I have to refer to variables by myModule.x instead of just x. If I do the above followed by “using myModule”, I get the same error as above.

How can I pull in a local module and use its namespace without having to specify the namespace before every one of its functions and variables?

The reason I want to do this is because this is what was done in the codebase from 0.6, and going through to add the namespace before every function and variable call would take forever, especially considering there are nested modules.

Welcome to Julia Discourse!

I am unable to replicate your error:

julia> write("myModule.jl", "module myModule export x; x = 1; end"); # create myModule.jl

julia> push!(LOAD_PATH, "./");

julia> using myModule
[ Info: Precompiling myModule [top-level]

julia> x

julia> rm("myModule.jl") # clean up

If you want to do it this way, push!(LOAD_PATH, "./") is unnecessary, and the using statement is slightly different.

using .myModule

is what you want.

Thank you! This seems to be working.

have a look also here, there are several examples that should make it clear why it works in that way…


Update for anyone who runs into this in the future. push!(LOAD_PATH, “./”); was being called on the highest “root” directory of my project. However, this dir does not actually contain any src code, it contains the directories that contain the src code.
The issue was push!(LOAD_PATH, “./”); is NOT recursive. So you need to make sure the path pointed to by ./ points to the exact directory you want to bring in, not a parent directory or anything like that.
In my case, doing push!(LOAD_PATH, “./src”) works.

Additional question: What is considered the current working directory changes depending on where you call the julia file. So if I do “julia ./src/main.jl” (or some other julia file), then the push!(LOAD_PATH, “./”) inside main.jl does not work because the current working directory is not the same as the file location. If I however cd into the correct directory, then call julia main.jl, it works. Is this intended behavior? Is there a way to have it push!(LOAD_PATH, “location of current file”) instead of the current working directory so the file can be called from anywhere?

The macro @__DIR__ gives you the location of the current file.

1 Like

Final post summarizing the solution:
To include local code to be able to use a local module, you need to use
push!(LOAD_PATH, “”);
If you try to use “./” or pwd() as the directory to include, it will dependent on where you call the julia file from. This is bad. Instead, use


which is a macro to get the location of the calling function. Using this will allow your code to be called from anywhere.

Thank you to everyone who helped me!