Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

julia

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.

like image 723
cmc Avatar asked Jan 03 '20 00:01

cmc


1 Answers

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/

like image 145
cmc Avatar answered Sep 18 '22 18:09

cmc