How do I add a local project to the import path in Julia?
Asked Answered
O

2

17

I want to be able to import or using a package that I'm writing in a directory ~/projects/ExamplePkg from my main Julia REPL / from another project or environment.

Oviduct answered 3/1, 2020 at 0:17 Comment(0)
O
16

By ]foo I mean "use the foo command at the Julia Pkg REPL". Type ] at the Julia REPL to enter the Pkg REPL. Use ]help <command name> for more info or check the link below.

Ensure that your package has a Project.toml that gives it a UUID and names it (generate one with ]generate from the Julia REPL or with the PkgTemplates package) and that it is in a git repo with at least one commit including all the relevant files.

Then choose how you would like to use the package.

You probably want to run ]dev ~/projects/ExamplePkg:

If dev is used on a local path, that path to that package is recorded and used when loading that package. The path will be recorded relative to the project file, unless it is given as an absolute path.

If you use dev and you change the dependencies in the dev'd package, then you should probably run ]resolve in all environments that depend on the package.

Or you can run ]add ~/projects/ExamplePkg:

Instead of giving a URL of a git repo to add we could instead have given a local path to a git repo. This works similarly to adding a URL. The local repository will be tracked (at some branch) and updates from that local repo are pulled when packages are updated. Note that changes to files in the local package repository will not immediately be reflected when loading that package. The changes would have to be committed and the packages updated in order to pull in the changes.

In Julia versions <1.4: If you accidentally ]add a package before the git repo is set up correctly then you might get ERROR: GitError(Code:EUNBORNBRANCH, Class:Reference, reference 'refs/heads/master' not found). Unfortunately, Julia will probably have cached the bad repo, and you will need to remove that from ~/.julia/clones/<gibberish>/. You can find the dir to remove with grep: $ grep ExamplePkg ~/.julia/clones/*/config.

Documentation: https://julialang.github.io/Pkg.jl/v1/managing-packages/

Oviduct answered 3/1, 2020 at 0:17 Comment(1)
I'm happy to say that the final claim (about the bad git state) is no longer accurate on Pkg master: github.com/JuliaLang/Pkg.jl/issues/1065. Pkg now checks the git repo before allowing you to add itBrister
K
3

you can try

path_to_package = "~/projects/ExamplePkg"
push!(LOAD_PATH,path_to_package)

# then use it, ExamplePkg is the package's name
using ExamplePkg 

But you have to run codes above whenever you restart Julia.

reference is Workflow tips-Julia Documentation

Knop answered 30/1, 2023 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.