# Using Printf and @printf in blocks

**URL:** <https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009>\
**Category:** New to Julia\
**Tags:** formatting\
**Created:** [September 16, 2018, 3:24am UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009 "2018-09-16T03:24:57Z")\
**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:** [September 16, 2018, 3:24am UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009/1 "2018-09-16T03:24:57Z")

</div>

A script like this:

```julia

using Printf
let
  @printf "%.2f" 3.14159
end

```

works fine.

However, if the “using Printf” is put inside the “let” block, an error occurs, as if Printf was not loaded:

```julia
let 
  using Printf
  @printf "%.2f" 3.14159
end

```

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

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 16, 2018, 8:01am UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009/2 "2018-09-16T08:01:24Z")

</div>

The block is compiled before it is executed, and Printf needs to be loaded before the block can be compiled.

(Putting a `using` statement inside a let block will not restrict the scope of the exported functions anyway.)

---

<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:** [September 16, 2018, 12:29pm UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009/3 "2018-09-16T12:29:57Z")

</div>

Thank you. It confuses me a little bit, on the other hand, that this works:

```julia
let
   include("./file.jl")
end

```

file.jl:

```julia
using Printf
@printf "%.2f" 3.14159

```

I am glad it works, otherwise the includes would not be portable, but that means that  
the include is not equivalent to the typing of the code in the same position.

---

<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 16, 2018, 12:39pm UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009/4 "2018-09-16T12:39:03Z")

</div>

> [@lmiq](#):
>
> that means that  
> the include is not equivalent to the typing of the code in the same position.

No it isn’t and can’t be. It’s equivalent to `eval`.

---

<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:** [September 16, 2018, 12:49pm UTC](https://discourse.julialang.org/t/using-printf-and-printf-in-blocks/15009/5 "2018-09-16T12:49:58Z")

</div>

Excellent. Thank you.
