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.
Foo
Foo
in a local Julia repository 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.
Baz
directory where I want to run Baz/script.jl
repl
, hit ]
and run dev --local PATH_TO_FOO
repl
, run using Foo
Foo
is now accessible in the current repl
session (or script)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.
By default, packages will be installed to ~/. julia/packages . To exit package mode, enter the Backspace key on an empty line.
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.
julia in the user's home folder. In Windows, it is located in the user's profile typically in C:\Users\[my-user-name] .
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:
Pkg.activate("ScriptEnvironment"; shared=true)
Pkg.add(Pkg.PackageSpec(; path="/path/to/Foo"))
(depending on your use case, you can also use Pkg.develop
, add registered packages, etc)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.
There are some instructions on registry creation and maintenance at https://github.com/HolyLab/HolyLabRegistry.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With