# Shortcut to load a package from another environment?

**URL:** https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620
**Category:** General Usage
**Tags:** packages, environment
**Created:** [July 14, 2023, 12:41pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620 "2023-07-14T12:41:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 14, 2023, 12:41pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620/1 "2023-07-14T12:41:21Z")

</div>

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):

```julia
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
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.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [July 14, 2023, 9:00pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620/2 "2023-07-14T21:00:19Z")

</div>

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

```julia
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
julia> @using_shared utils PrettyTables
julia> pretty_table(["OK"])
┌────────┐
│ Col. 1 │
├────────┤
│ OK │
└────────┘

```

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [July 14, 2023, 9:12pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620/3 "2023-07-14T21:12:48Z")

</div>

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:

```julia
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
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!

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [July 15, 2023, 5:37pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620/4 "2023-07-15T17:37:26Z")

</div>

> [@lmiq](#):
>
> 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

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…

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 15, 2023, 5:41pm UTC](https://discourse.julialang.org/t/shortcut-to-load-a-package-from-another-environment/101620/5 "2023-07-15T17:41:51Z")

</div>

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.
