Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a local Julia package in a local registry?

Tags:

package

julia

I have a Julia package in my home directory under the name Foo. Foo is a directory with Project.toml file that describes the package dependences, name, etc. I want to be able to use this package from another folder in a particular manner as follows.

  1. Build the package Foo
  2. Register Foo in a local Julia repository
  3. Run Pkg.add("Foo") from anywhere on the system, such as script.jl which would have the following:
using Pkg
Pkg.add("Foo")
using Foo

# Now use Foo
Foo.bar()

Here is what I've tried so far.

  • Navigate to Baz directory where I want to run Baz/script.jl
  • Use repl, hit ] and run dev --local PATH_TO_FOO
  • From repl, run using Foo
  • Foo is now accessible in the current repl session (or script)
  • Summary: I have managed to import a package Foo in another directory Baz.

Basically, I want to be able to write a script.jl that can make use of the local registry instead of this dev --local . method.

like image 423
Neil Avatar asked Sep 16 '19 23:09

Neil


People also ask

Where are Julia packages stored?

By default, packages will be installed to ~/. julia/packages . To exit package mode, enter the Backspace key on an empty line.

What is a Julia registry?

Package registries are used by Julia's package manager Pkg. jl and includes information about packages such as versions, dependencies and compatibility constraints. The General registry is open for everyone to use and provides access to a large ecosystem of packages.

Where are Julia packages stored on Windows?

julia in the user's home folder. In Windows, it is located in the user's profile typically in C:\Users\[my-user-name] .


2 Answers

I assume that this is for personal use and you are not a system administrator for a large group.

Maintaining a registry is hard work. I would avoid creating a registry if at all possible. If your goal is to make the use of scripts less painful, a lighter solution exists: shared environments.

A shared environment is a regular environment, created in a canonical location. This means that Pkg will be able to locate it by name: you do not have to specify an explicit path.

To set up a shared environment:

  • give it a name: Pkg.activate("ScriptEnvironment"; shared=true)
  • populate it: Pkg.add(Pkg.PackageSpec(; path="/path/to/Foo")) (depending on your use case, you can also use Pkg.develop, add registered packages, etc)
  • that's all!

Now all your scripts can use the shared environment:

using Pkg
Pkg.activate("ScriptEnvironment"; shared=true)
using Foo

If other sets of scripts require different sets of packages, repeat the procedure with a different shared name. Pkg environments are really cheap so feel free to make liberal use of this approach.


Note: I would discourage beginning scripts with Pkg.add("Foo") because this carries the risk of inadvertently polluting the active environment.

like image 104
David Varela Avatar answered Sep 20 '22 17:09

David Varela


There are some instructions on registry creation and maintenance at https://github.com/HolyLab/HolyLabRegistry.

like image 39
tholy Avatar answered Sep 20 '22 17:09

tholy