Best practice: put all using in the main [package].jl?

I am developing a package named EOS.jl. The main file EOS.jl mainly includes other source files.
I am wondering what is the best practice for the using of other registered packages. Should I put all using at the beginning of EOS.jl (see below)?

module EOS
using DelimitedFiles, GLPK, JuMP, Cbc
using LinearAlgebra, FillArrays
using DataFrames, Bijections, TOML
using LightGraphs # used only in milp.jl

However, most packages are used exclusively in one source file. For example, the LightGraphs is used only in the milp.jl source file. Shall I put using LightGraphs in EOS.jl or in milp.jl?

1 Like

Typically, people put all using and include statements in the main file, i.e. EOS.jl in your case.

1 Like

include is rather simple command, you can think about it as a placeholder, where actual code appear during preprocessing step.

It means, that if you have three files

# main.jl
include("foo.jl")
include("bar")

# foo.jl
using A
using B

<some foo related stuff>

# bar.jl
using C
using D

<some bar related stuff>

then during preprocess stage, main.jl actually explode to

using A
using B

<some foo related stuff>
using C
using D

<some bar related stuff>

and only after that compiling starts (it’s not exact picture, but good enough as a zero approximation). So, as you can see, there is no “exclusive usage of package in a source file”, since all of them are just glued together. Because of this question where to put using declarations is only important from the readability point of view (i.e. how you read your source files - from the main file or file by file). As it was said in a previous comment, usually people put everything in top file (EOS.jl in your case), since it is easier to read.

1 Like

Either way is fine, both have advantages. Putting these statements in the main file gives the reader of the code an overview of all namespace management, while putting them where they are used keeps things together.

Most packages do the first, but don’t worry too much about these things, just keep coding, you can always change these things trivially.

2 Likes