Julia Import Versus Using
Import is a way to bring one chosen name from a module into the current code. Using brings in the names a module marks for sharing.
Import brings one chosen name
This block imports only mean from Statistics. The short call finds the average of three numbers.
import Statistics: mean
println(mean([10, 20, 30]))Using brings the shared names
This block uses Statistics. Both mean and median can be called by their short names.
using Statistics
println(mean([1, 3]))
println(median([1, 3, 5]))Import and using work side by side
This block imports mean and uses Dates. Both short calls work in the same script.
import Statistics: mean
using Dates
println(mean([4, 6]))
println(year(Date(2025, 5, 1)))Example
import Statistics: mean
using Dates
println(mean([2, 4, 6]))
println(month(Date(2025, 5, 1)))Key points
- Chosen name: an import brings one chosen name from a module into the current code.
- Short call: an imported name can be called by its short name.
- Shared names: using brings in the names a module marks for sharing.
- Exported name: a name a module marks for sharing.
- Both forms: import and using can be used in the same script for different modules.
In this example, mean is imported from Statistics and Dates is used. The first line prints the average of 2, 4, and 6. The second line prints the month of the date. One script uses both forms.
Note
import brings one name, while using brings the shared names.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
After import Statistics: mean, which name can be called by its short name?