# Macros not imported within blocks

**URL:** <https://discourse.julialang.org/t/macros-not-imported-within-blocks/47009>\
**Category:** General Usage\
**Created:** [September 21, 2020, 1:49pm UTC](https://discourse.julialang.org/t/macros-not-imported-within-blocks/47009 "2020-09-21T13:49:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [September 21, 2020, 1:49pm UTC](https://discourse.julialang.org/t/macros-not-imported-within-blocks/47009/1 "2020-09-21T13:49:02Z")

</div>

I find that macros are not imported if the package defining the macros are imported within a block. For example, importing `Printf` and using the macro `@printf` by

```julia
if true
    using Printf
    @printf("test")
end

```

produces the following error:

```julia
ERROR: LoadError: UndefVarError: @printf not defined

```

The same error is generated when `if true ... end` block is replaced with `begin ... end` block.

The same error is produced for packages other than `Printf`. For example,

```julia
begin
    using StaticArrays
    @SVector(zeros(3))
end

```

produces the following error:

```julia
ERROR: LoadError: UndefVarError: @SVector not defined

```

Non-macros are imported fine. For example,

```julia
begin
    using StaticArrays
    SVector(1,2,3)
end

```

runs fine.

I observe the same behavior in Julia version 1.0 through 1.5. Why is this happening? Is there a way to import macros properly within code blocks? I would like to import some packages within `if ... end` blocks because importing packages takes time: I don’t want to import them when they are not needed.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 21, 2020, 2:51pm UTC](https://discourse.julialang.org/t/macros-not-imported-within-blocks/47009/2 "2020-09-21T14:51:04Z")

</div>

Macros operates per top level block. You can only use the macro in the following block.
