How to load a module from the current directory?

#1

I thought it sufficed to add your current working directory to LOAD_PATH to use modules from there, but apparently that is not so. Is the only way to get local code (for small scale dev/testing) to use include? I could swear this worked last week…

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca (2019-01-21 21:24 UTC)
...

julia> LOAD_PATH
4-element Array{String,1}:
 "@"                                       
 "@v#.#"                                   
 "@stdlib"                                 
 "/l/tests/julia"

julia> pwd()
"/l/tests/julia"

julia> readdir()
31-element Array{String,1}:
...
 "MyMod.jl"              
 "MyMod.jl~"             
...

julia> using MyMod
ERROR: ArgumentError: Package MyMod not found in current path:
- Run `import Pkg; Pkg.add("MyMod")` to install the MyMod package.

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:823

julia> 
0 Likes

#2

I think you need to explicitly include("MyMod.jl"), and then possibly using .MyMod (since it’s just a module, not a package).

0 Likes

#3

That all looks fine to me, and I checked that it works on my machine.

In situations like this, I find it useful to try to isolate the smallest difference between the broken example and a working example. For example, I would sanity check by creating a new, empty folder, creating a trivial file in that folder like:

# File: MyMod.jl
module MyMod

x = 1

end

and then running (in Julia):

julia> push!(LOAD_PATH, pwd())
4-element Array{String,1}:
 "@"                                    
 "@v#.#"                                
 "@stdlib"                              
 "/my/folder/with/just/that/one/file"

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

julia> MyMod.x
1

once you have a working example, it will be easier to figure out what the difference is between that and your (currently broken) example.

1 Like

#4

Thank you for your answer!

The issue was that I had an empty Project.toml file lying around in that one directory. I’m only at the stage of playing around with Modules, Packages, Projects and everything, so I’m breaking things. :wink:

Would you or anybody know of a good introduction to the above subjects?

0 Likes