How to load a module from the current directory?

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> 
2 Likes

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

8 Likes

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.

4 Likes

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?

5 Likes

Did you ever find the introduction you were looking for? I could really use one too…

1 Like

I don’t think this particular behaviour is documented yet. If it is, it wasn’t visible enough for me to find it: I as well struggled with this issue a few months ago.

The confusion

In particular, I had to look through Julia’s code to understand that any entry in LOAD_PATH, whose directory contains a Project.toml file, will be treated as a “project environment” only. In other words: having a Project.toml file inhibits Julia from using it as a “package repository”.

An introduction, for now

At the moment, I would suggest you look at some of the posts I have written, starting with the “tips” found here:
Proper way of organizing code into subpackages - #3 by MA_Laforge

I also suggest you read the two posts of mine that directly follow it. I tried my best to give useful insight.

You might also be interested to take a look at the improved package/module documentation (v1.7):
Modules · The Julia Language

Overall though, I think the package/module documentation still needs some improvement (I just haven’t figured out how, exactly).

11 Likes