# How to use macros to install and load a package?

**URL:** <https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076>\
**Category:** Package Management\
**Tags:** packages, macros\
**Created:** [May 13, 2021, 11:27am UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076 "2021-05-13T11:27:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 13, 2021, 11:27am UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/1 "2021-05-13T11:27:36Z")

</div>

I was thinking of writing something like

```julia
macro use(ex)
    quote
        Pkg.add(string($ex))
        using $ex
    end
end

```

but I fear that’s not how to use it.

How do I write a macro taking in a nonexisting module name so that it can generate code that installs it?

```julia
@use Symbolics

```

Bonus task: check if package is already installed.

The main use case would be to use it inside Pluto, but I could image it to be quite useful in general.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 13, 2021, 11:47am UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/2 "2021-05-13T11:47:10Z")

</div>

Julia v1.7 will have this feature built-in:  
[https://github.com/JuliaLang/julia/pull/39026](https://github.com/JuliaLang/julia/pull/39026)

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 13, 2021, 12:01pm UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/3 "2021-05-13T12:01:19Z")

</div>

As I see this it would only work in the REPL, but does not provide a general option to implement this in other front ends like Pluto.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [May 13, 2021, 12:18pm UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/4 "2021-05-13T12:18:20Z")

</div>

Pluto has it’s own version of the feature in the works, by the way: [https://github.com/fonsp/Pluto.jl/pull/844](https://github.com/fonsp/Pluto.jl/pull/844)

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 13, 2021, 1:04pm UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/5 "2021-05-13T13:04:30Z")

</div>

What I am struggling with is how do I generate a string from a Symbol passed to a macro.  
Would this be the correct way of implementing it?

```julia
macro use(pkg)
    str = String(pkg)
    quote
        Pkg.add($str)
        using $pkg
    end
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 13, 2021, 1:10pm UTC](https://discourse.julialang.org/t/how-to-use-macros-to-install-and-load-a-package/61076/6 "2021-05-13T13:10:18Z")

</div>

Not sure what’s going on with that. Maybe you are forgetting an `esc`. I always write macros as `esc` from a helper function to avoid those issues.

```julia
julia> macro use(pkg)
           esc(use_helper(pkg))
       end;

julia> function use_helper(pkg)
           p = string(pkg)
           quote 
               import Pkg; 
               Pkg.add($p)
               using $pkg
           end
       end;

```
