Search code examples
packagejuliajupyter-lab

Minimal Julia package with imports?


How do I generate a Julia package with a dependency on ForwardDiff (for example)?

I am trying, and failing, to create a package with dependencies in Julia. I am trying to follow the official documentation here. My minimal non-working example is as follows. I type these commands in Jupyter Lab, I'm on Julia 1.9.3.

If you don't want to read all of this, all I do is use Pkg's generate command to generate an empty project Foo. I then add using ForwardDiff to Foo.jl, and add ForwardDiff as a dependency via ]add ForwardDiff. I've activated Foo and everything. As soon as I using Foo, I get error messages. I can't fix it.

I'm aware of PkgTemplates. I've tried it, same problem.

]generate Foo
  Generating  project Foo:
    Foo/Project.toml
    Foo/src/Foo.jl
]activate Foo
Activating project at `~/.../Foo`
]add ForwardDiff
   Resolving package versions...
    Updating `~/.../Foo/Project.toml`
  [f6369f11] + ForwardDiff v0.10.36
...
;cat Foo/src/Foo.jl
module Foo
using ForwardDiff
greet() = print("Hello World!")

end # module Foo
> using Foo
[ Info: Precompiling Foo [b793ecfc-1afd-4979-b49f-7079086f47c4]
ERROR: LoadError: ArgumentError: Package ForwardDiff [f6369f11-7733-5829-9624-2563aa707210] is required but does not seem to be installed:
 - Run `Pkg.instantiate()` to install all recorded dependencies.
> using Pkg; Pkg.instantiate()
Precompiling project...
  ✗ Foo
  0 dependencies successfully precompiled in 1 seconds. 19 already precompiled.
  1 dependency errored. To see a full report either run `import Pkg; Pkg.precompile()` or load the package
...
]resolve
  No Changes to `~/Dropbox/2023/Spectral Barrier Method/julia/Foo/Project.toml`
  No Changes to `~/Dropbox/2023/Spectral Barrier Method/julia/Foo/Manifest.toml`
]instantiate
(nothing happens)
> using Foo
[ Info: Precompiling Foo [b793ecfc-1afd-4979-b49f-7079086f47c4]
ERROR: LoadError: ArgumentError: Package ForwardDiff [f6369f11-7733-5829-9624-2563aa707210] is required but does not seem to be installed:
 - Run `Pkg.instantiate()` to install all recorded dependencies.
...

I've also tried doing various kernel restarts to try and get Julia to do something different. Depending on how many times, and when you restart, you get different error messages.


Solution

  • Solution was found on the Julia discourse boards.

    Solution was found by several people, I’ve credited nasjko. My main problem was that LOAD_PATH and JULIA_LOAD_PATH can interact in a confusing manner with package loading, and apparently their use is discouraged. I had . in my JULIA_LOAD_PATH. Once I deleted it from the path, I was able to do using MyPackage. Details in the thread below.

    https://discourse.julialang.org/t/streamlined-way-of-making-a-package/104682/3