# How to output a local import from a macro

**URL:** <https://discourse.julialang.org/t/how-to-output-a-local-import-from-a-macro/75307>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [January 27, 2022, 4:58pm UTC](https://discourse.julialang.org/t/how-to-output-a-local-import-from-a-macro/75307 "2022-01-27T16:58:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [January 27, 2022, 4:58pm UTC](https://discourse.julialang.org/t/how-to-output-a-local-import-from-a-macro/75307/1 "2022-01-27T16:58:20Z")

</div>

Trying to output a local import statement. The export statement works fine, but can’t figure out local import inside or outside quote. I tried running esc, QuoteNode, symbol… all sorts of combinations.

```julia
macro testmacro(T)
    name = T
    tname = Symbol(name, "Extra")
    blk = quote
        module $(esc(name))
            $(esc(tname)) = "test"
        end
        import . $(esc(name)) : $(esc(tname)) # <- doesn't work
        export $(esc(name)), $(esc(tname))
    end
    # push!(blk.args, :(import $(Symbol(".$(modname):")) $(typename))) # <- doesn't work
    blk.head = :toplevel
    return blk
end

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 27, 2022, 5:33pm UTC](https://discourse.julialang.org/t/how-to-output-a-local-import-from-a-macro/75307/2 "2022-01-27T17:33:17Z")

</div>

I think it’s because of your extra spacing around the `.`. This seems to work:

```julia
julia> mod = :MyModule
:MyModule

julia> f = :sin
:sin

julia> :(import .$mod: $f)
:(import .MyModule: sin)

```

Once in a while, I get stuck with this kind of problem. If I can’t write the expression with `quote`, I just `dump` what I want to produce, and build it “manually” with `Expr` and such. It’s uglier, but it always works.

```julia
julia> dump(:(import .MyModule: sin))
Expr
  head: Symbol import
  args: Array{Any}((1,))
    1: Expr
      head: Symbol :
      args: Array{Any}((2,))
        1: Expr
          head: Symbol .
          args: Array{Any}((2,))
            1: Symbol .
            2: Symbol MyModule
        2: Expr
          head: Symbol .
          args: Array{Any}((1,))
            1: Symbol sin

```

As an aside, reading your code, I would just `esc(quote ... end)`, instead of escaping individual elements…
