The cookiecutter
equivalent for Julia is PkgTemplates
(github link), but you can do a ton with the base Pkg
library. As someone who really like kedro
for python (kedro docs), I really want to know the best methods of creating a modular, reproducible analysis using Julia.
Here's the steps I need to understand:
Surprisingly, the best documentation for setting up your project exists in the Pkg
Getting Started With Environments documentation itself.
Instead of creating new environments from the command line with conda
, venv
, pyenv
, poetry
, etc etc you can do it through the Julia REPL or calling the Pkg
library within your script.
Pkg.status()
: See what environment you're usingPkg.activate()
: Activate environmentPkg.generate()
: GeneratesPkg.instantiate()
:using Pkg;
Pkg.status()
Status `~/.julia/environments/v1.6/Project.toml` [336ed68f] CSV v0.9.11 [8f4d0f93] Conda v1.5.2 [a93c6f00] DataFrames v1.2.2 [1313f7d8] DataFramesMeta v0.10.0 [c91e804a] Gadfly v1.3.4 [cd3eb016] HTTP v0.9.17 [7073ff75] IJulia v1.23.2 [91a5bcdd] Plots v1.24.2 [c3e4b0f8] Pluto v0.17.2 [438e738f] PyCall v1.92.5 [6f49c342] RCall v0.13.12 [ce6b1742] RDatasets v0.7.6 [fdbf4ff8] XLSX v0.7.8
pwd
- prints the current working directoryreaddir
- just like ls
in bash, lists files in the current directorymkdir
- makes a new directoryrm(path, recursive=true)
- recursively remove a directoryreaddir()
2-element Vector{String}: ".ipynb_checkpoints" "A Julia Workflow.ipynb"
Pkg.generate
¶Julia gives us a barebones package generator with generate
that will make a Project.toml
config file and a src
directory with a 'hello world' julia file in it.
Pkg.generate("my_package")
Generating project my_package:
my_package/Project.toml
my_package/src/my_package.jl
Dict{String, Base.UUID} with 1 entry: "my_package" => UUID("a2f78104-38a4-4b74-bda0-0fd91114a85c")
See the directory structure of my_package
:
readdir("my_package")
2-element Vector{String}: "Project.toml" "src"
readdir("my_package/src")
1-element Vector{String}: "my_package.jl"
Activate the new package after creating the strawman using Pkg.generate
:
Pkg.activate("my_package")
Pkg.status()
Activating environment at `~/Desktop/jul_test/workflow/my_package/Project.toml`
You can also open a REPL using your project by navigating to the my_package
directory and calling something like:
# bash
julia --project=. # the '.' says to open julia using the environment in the current directory
And finally, if you open a jupyter notebook in the my_package
directory, it should use the directory's Julia environment by default.
Loading someone else's project after you've activated it, use instantiate
:
Pkg.instantiate()
Precompiling project... ✓ my_package 1 dependency successfully precompiled in 1 seconds
Pkg.status()
Project my_package v0.1.0 Status `~/Desktop/jul_test/workflow/my_package/Project.toml` (empty project)
Import your package
using my_package
And call the single function in the package
my_package.greet()
Hello World!
Pkg
¶generate
: Create a new, barebones julia package structureactivate
: Activates a julia environment (or creates one if it doesn't already exist)instantiate
: Install the package and required dependenciesrm("my_package", recursive=true)
readdir()
2-element Vector{String}: ".ipynb_checkpoints" "A Julia Workflow.ipynb"