`include` calls in a Julia package

Assume we are developing a package called MyPkg. Step 3 of this tutorial states that, in order to export our package functions, we should add the following to our src/MyPkg.jl file:

  • A statement export my_function
  • A statement include("file_with_functions.jl")

Fair enough. However, in a complex package, we may have many inter-dependent files. Say our src folder has files A.jl and B.jl, and say B.jl uses functions from A.jl and thus contains the statement include("A.jl").

Following the instructions of the tutorial, we should write the following in our MyPkg.jl file:

export first_function_of_A
...
export last_function_of_A
export first_function_of_B
...
export last_function_of_B

include("A.jl")
include("B.jl")

However, since B.jl already has an expression include("A.jl"), this leads to a double evaluation of this expression. When importing the package, we will get Warning: Replacing docs for... for all functions of A.jl.

What is the best practice for structuring the MyPkg.jl file in the context of multiple files, some of which source (include) others? The practice should not only avoid repeated calls to include but ensure that everything is exported in the package.

Thanks!

B.jl should NOT contain any include statement. Not needed.

1 Like

One common way is to write MyPkg.jl like so:


# External packages used anywhere
using Arrow
using DataFrames

#exports
export function1
export function2

# Internal files
include("A.jl")
include("B.jl")

Since A.jl will already have been included, B.jl will have access to its code. So you don’t need to worry about reimporting A in B.

5 Likes