Easier way to load package I'm developing?

Hi, new to Julia.

I have this package I want to develop which for the sake of example we’ll call myPkg. It’s located in ~/.julia/dev/myPkg and I’ve used PkgTemplates.jl to set up properly.

I’m using Juno through Atom to run my Julia code. When I start up the REPL there I verify that I’m in the correct path:

julia> pwd()
"~/.julia/dev.myPkg"

but when I attempt to load my module from the REPL, I get the following error:

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

Now I have found a workaround to this by updating LOAD_PATH:

julia> push!(LOAD_PATH, pwd())
4-element Array{String,1}:
 "@"
 "@v#.#"
 "@stdlib"
 "~/.julia/dev/myPkg"

And after doing this I’m able to load myPkg. My question is, why wasn’t this possible for me before when I was already in the correct directory? At face value it’s a minor problem but I have the impression I’m misunderstanding fundamental.

using myPkg traverses traverses through LOAD_PATH and searches the folders listed to see if it can find such a package. There are some default paths as you can see, e.g. "@v#.#" that tells the pkg system to look into the Project.toml file of the v#.# environment and see if it can find an entry to your package there. The package system does not consider pwd() as a directory where the package could be located by default, so thats why you had to push to LOAD_PATH. I don’t think this is really a good approach unless you are familiar with the Julia package/environment system.

There are two alternative ways. First the more canonical way would be to use dev.

(@v1.4) pkg> dev mypkg
Path `/home/affan/.julia/dev/mypkg` exists and looks like the correct package. Using existing path.
  Resolving package versions...
   Updating `~/.julia/environments/v1.4/Project.toml`
  [c6683dd1] + mypkg v0.1.0 [`~/.julia/dev/mypkg`]
   Updating `~/.julia/environments/v1.4/Manifest.toml`
  [c6683dd1] + mypkg v0.1.0 [`~/.julia/dev/mypkg`]

julia> using mypkg
[ Info: Precompiling mypkg [c6683dd1-c332-45c4-be08-9497673428c9]

julia>

So by dev'ing the package, it adds an entry to your Project.toml file with the package information (see ] st to see the dev'ed package info). This will let you run using mypkg regardless of your pwd. Note the entry for the dev'ed package is added to the Project file of the currently active environment.

The second way would be to manually include the file that defines the package module.

include("file_that_defines_mypkg.jl") # often just myPkg.jl 
using .myPkg 
3 Likes

Great answer. Thanks a million!

Because you were in a different directory than your code loader was :wink: This can easily catch you out.

julia> using Pkg

julia> Pkg.status()
Status `~/.julia/environments/v1.5/Project.toml`

julia> Base.load_path()
2-element Array{String,1}:
 "/Users/example/.julia/environments/v1.5/Project.toml"
 "/Applications/Julia-1.5.app/Contents/Resources/julia/share/julia/stdlib/v1.5"

One solution is:

julia> Pkg.activate(pwd())
 Activating environment at `~/.julia/dev/myPkg`

Alternatively, run julia --project in the package directory. That has the same effect. From now on, "@" on the load path refers to ~/.julia/dev/myPkg.

julia> Pkg.status()
Project myPkg v0.1.0
Status `~/.julia/dev/myPkg/Project.toml`
...

julia> Base.load_path()
3-element Array{String,1}:
 "/Users/example/.julia/dev/myPkg/Project.toml"
 "/Users/example/.julia/environments/v1.5/Project.toml"
 "/Applications/Julia-1.5.app/Contents/Resources/julia/share/julia/stdlib/v1.5"

The code loader knows where to find the active package, and using myPkg will now work.

2 Likes