Printf Error in Custom Package

Hi all,

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

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?

Can you provide a mwe?

I cannot reproduce that as it is:

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> 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"


1 Like

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.

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().