Julia Package Basics
A package is a reusable project that other projects can use. Package basics cover starting a project and adding packages to it.
A package project starts with a project file
This block creates a ShopApp folder and writes its project file. Reading it back prints the name and the unique id.
mkpath("ShopApp")
write("ShopApp/Project.toml", "name = \"ShopApp\"\nuuid = \"66666666-6666-6666-6666-666666666666\"")
println(read("ShopApp/Project.toml", String))An added package appears under deps
This block writes a project file for a project with one added package. Reading it back prints the package line under deps.
mkpath("CartApp")
write("CartApp/Project.toml", "name = \"CartApp\"\nuuid = \"77777777-7777-7777-7777-777777777777\"\n[deps]\nGreeterKit = \"88888888-8888-8888-8888-888888888888\"")
println(read("CartApp/Project.toml", String))One added package can be read back on its own
This block writes a project file first. It then prints only the package line.
mkpath("BasketApp")
write("BasketApp/Project.toml", "name = \"BasketApp\"\nuuid = \"99999999-9999-9999-9999-999999999999\"\n[deps]\nGreeterKit = \"88888888-8888-8888-8888-888888888888\"")
lines = readlines("BasketApp/Project.toml")
println(lines[4])Example
mkpath("BakeryApp")
write("BakeryApp/Project.toml", "name = \"BakeryApp\"\nuuid = \"10101010-1010-1010-1010-101010101010\"\n[deps]\nOvenKit = \"20202020-2020-2020-2020-202020202020\"\nFlourKit = \"30303030-3030-3030-3030-303030303030\"")
println(read("BakeryApp/Project.toml", String))Key points
- Package: a reusable project that other projects can use.
- Activate: choose a project so new packages are added to that project.
- Add: adding a package records it in the chosen project.
- Deps record: after a package is added, its name appears under deps in Project.toml.
- Separate code: a package keeps its own code apart from the project that uses it.
In this example, a BakeryApp folder is created and a project file is written inside it. The file names the project and gives it a unique id. Two added packages appear under deps. Reading the file back prints all five lines.
Note
a package is added to the chosen project, not to every project at once.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does it mean to activate a project?