# Printf Error in Custom Package

**URL:** <https://discourse.julialang.org/t/printf-error-in-custom-package/69211>\
**Category:** General Usage\
**Created:** [October 4, 2021, 11:02pm UTC](https://discourse.julialang.org/t/printf-error-in-custom-package/69211 "2021-10-04T23:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)\
**Post date:** [October 4, 2021, 11:02pm UTC](https://discourse.julialang.org/t/printf-error-in-custom-package/69211/1 "2021-10-04T23:02:51Z")

</div>

Hi all,

I’m setting up a private package that has the following module definitions

```julia
Module MainMod

Using Printf
...

include("paths to submodules")
...

using ..Submodules # Will this let me directly call fxns from top-level module?
...

end

```

I believe I have correctly set up the module files and a local private registry on which this package will live. However, when I go to type `Using TestPackage` it throws the error ` LoadError: @printf not defined '.

The `@printf` macro is used in a couple functions across the submodules, and this seems to be a known problem. The solutions I found of doing `]up` and/or `]resolve` do not seem to fix the issue.

I have thrown `Using Printf` statements in every module/submodule file, to no avail.

Does anyone have any insight into how to solve this problem? Is it related to the compilation of macros before `Using Printf`?

---

<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:** [October 5, 2021, 12:20am UTC](https://discourse.julialang.org/t/printf-error-in-custom-package/69211/2 "2021-10-05T00:20:29Z")

</div>

Can you provide a mwe?

I cannot reproduce that as it is:

```julia
julia> module A
           using Printf
           s = @sprintf "test"
       end
Main.A

julia> using .A

julia> A.s
"test"

julia> module B
           using ..A
       end
Main.B

julia> using .B

julia> B.A.s
"test"

```

If you want to use `@printf` in a module that uses `A`, without “reusing” in the new you need to export it in `A`, for example (although I don’t know if I would recommend that, just use it again and make the module more self contained):

```julia
julia> module C
           using ..A
           s = @sprintf "testC"
       end
ERROR: LoadError: UndefVarError: @sprintf not defined
in expression starting at REPL[7]:3

julia> module A
           using Printf
           export @sprintf
           s = @sprintf "test"
       end
WARNING: replacing module A.
Main.A

julia> module C
           using ..A
           s = @sprintf "testC"
       end
WARNING: replacing module C.
Main.C

julia> using .C

julia> C.s
"testC"

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [October 5, 2021, 7:05am UTC](https://discourse.julialang.org/t/printf-error-in-custom-package/69211/3 "2021-10-05T07:05:13Z")

</div>

> [@graham](#):
>
> The `@printf` macro is used in a couple functions across the submodules, and this seems to be a known problem.

If you use `@printf` in a submodule, you need to do `using Printf` in that submodule. Any module by default only imports `Base`, it doesn’t inherit imports from its parent module.

> [@graham](#):
>
> `using ..Submodules # Will this let me directly call fxns from top-level module?`

This should be `using .Submodule` (only one dot, not two). Regarding the question in the comment, `using .Submodule` will let you call exported functions from `Submodule` directly in `MainMod`, i.e., if you have `export foo` in `Submodules` then you can call `foo()` directly in `MainMod`. Otherwise, you need to qualify it, i.e., call `Submodule.foo()`.
