Shortcut to load a package from another environment?

I more and more use this workflow to load some packages that are not part of a current development project, but are useful for testing (Plots, LiveSever, for example):

mypkg> activate @LiveServer # shared environment with LiveServer installed

LiveServer> <backspace> using LiveServer

LiveServer> activate . # return to project environment

julia> servedocs()

Is there any package that provides a shortcut to that, something like:

julia> using LiveServer@LiveServer # use LiveServer from the shared @LiveServer environment

That would facilitates the use of specific shared packages without bloating the Main environment.

Not the syntax you’re looking for, but I sometimes use this:

import Pkg

macro using_shared(shared_env, pkgnames)
    @assert shared_env isa Symbol
    shared_env = String(shared_env)

    if pkgnames isa Symbol
        pkgnames = [pkgnames]
    else
        @assert pkgnames.head == :tuple
        pkgnames = pkgnames.args
    end

    using_expr = Expr(:using)
    using_expr.args = [Expr(:., pkg) for pkg in pkgnames]

    quote
        const cpp = $Pkg.project().path
        try
            $Pkg.activate($shared_env, shared=true, io=devnull)
            $using_expr
        finally
            $Pkg.activate(cpp, io=devnull)
        end
    end
end

For example, I have defined a shared environment named @utils, in which I installed PrettyTables. I can use it like so:

julia> @using_shared utils PrettyTables
julia> pretty_table(["OK"])
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Col. 1 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     OK β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
4 Likes

FWIW, I very recently started to try another approach: instead of temporarily activating the shared environment, I add it to the environment stack if I happen to need to use a package in it:

import Pkg
function stack_shared_env!(shared_env)
    cpp = Pkg.project().path
    try
        Pkg.activate(shared_env; shared=true, io=devnull)
        shared_env_path = Pkg.project().path
        if shared_env_path βˆ‰ LOAD_PATH
            push!(LOAD_PATH, shared_env_path)
        end
    finally
        Pkg.activate(cpp, io=devnull)
    end
    nothing
end

Same example as above:

julia> stack_shared_env!("utils")
julia> using PrettyTables
julia> pretty_table(["OK"])
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Col. 1 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     OK β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

I haven’t used this for long, but for now I’m happy with this technique. If anyone happens to know about something wrong with this approach, please let me know!

5 Likes

I also find myself more and more in need of tools like this too. Another use case is when I develop a package with extensions, and I want to load the weak dependency to check whether everything works well.

Anyway, do you think it would be useful to make the @using_shared macro more widely available to the community? It does feel small for a package, however…

Actually I feel something like this should be in base, associated with starting by default in a temporary environment, and stimulating a more environment-based workflow in general.

2 Likes