-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg.Rmd
More file actions
231 lines (180 loc) · 7.29 KB
/
Copy pathpkg.Rmd
File metadata and controls
231 lines (180 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
title: "Pkg.jl: Julia package management for fun and profit"
author: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>
---
# Basics
What happens when you type `using MixedModels`? How does Julia know what bit of
code to run? Where does it look to find this code? Which version is it using?
The answers to these questions are the province of the "package manager",
Pkg.jl. As always, the [official
documentation](https://docs.julialang.org/en/v1/stdlib/Pkg/index.html) is the
best source of infromation. But in the interest of expediency, here is a cheat
sheet.
## Projects in Julia
A project in Julia is like a packrat project (R) or virtualenv (python).
It is a list of the packages that are used for running some bit of julia code.
Any directory can be a project, as long as it has a
[Project.toml](#Project.toml) file in it.
## Project.toml
Lists the **dependencies** for a project.
A basic Project.toml has one section, that lists the dependencies like this:
```
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
```
This is how Julia knows what you mean when you say `using CSV`. `CSV` is just a
convenient, human-readable shorthand for the "real" name of the package, which
is the UUID (Universally Unique ID), `336ed68f-0bac-5ca0-87d4-7b16caf5d00b`.
You **rarely if ever need to edit `Project.toml` by hand**. It is automatically
generated by the package manager the vast majority of the time, using commands
in **Pkg mode**.
## Pkg mode
To enter package mode, press `]`. The `julia>` prompt should change to a pkg
prompt, like `(v1.3) pkg>`.
The bit in the parentheses gives the name of the current project environment.
The **default environment** is the global environment for the current Julia version
(in this case, 1.3).
### Creating and activating a project environment
But any directory can be a project. To **activate** a project in a directory,
you do:
```julia-repl
julia> # press ] to enter Pkg mode...
(v1.3) pkg> activate path/to/your/project/directory
Activating new environment at `~/path/to/your/project/directory/Project.toml`
(directory) pkg>
```
Noticed that the Pkg mode prompt now shows that you're in the new project (it's
named after the directory. Now, whenever you do an Pkg operations, they apply
to this project.
### Adding required packages to your project
You need to `add` every package that you directly import with `using` or
`import` in your scripts/jmd files:
```julia-repl
(directory) pkg> add StatsBase
Resolving package versions...
Updating `~/path/to/your/project/directory/Project.toml`
[2913bbd2] + StatsBase v0.32.1
Updating `~/path/to/your/project/directory/Manifest.toml`
[9a962f9c] + DataAPI v1.1.0
[864edb3b] + DataStructures v0.17.9
[e1d29d7a] + Missings v0.4.3
[bac558e1] + OrderedCollections v1.1.0
[a2af1166] + SortingAlgorithms v0.3.1
[2913bbd2] + StatsBase v0.32.1
[2a0f44e3] + Base64
[8ba89e20] + Distributed
[b77e0a4c] + InteractiveUtils
[8f399da3] + Libdl
[37e2e46d] + LinearAlgebra
[56ddb016] + Logging
[d6f4376e] + Markdown
[de0858da] + Printf
[9a3f8284] + Random
[9e88b42a] + Serialization
[6462fe0b] + Sockets
[2f01184e] + SparseArrays
[10745b16] + Statistics
[8dfed614] + Test
[4ec0a83e] + Unicode
```
Two things have happened: first, the new Project.toml lists `StatsBase` as a
dependency:
```
[deps]
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
```
Second, a **Manifest.toml** file has been created, which lists the exact
versions of all packages that have been installed to satisfy the requirements of
this project.
```
# This file is machine-generated - editing it directly is not advised
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[DataAPI]]
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.1.0"
[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "b7720de347734f4716d1815b00ce5664ed6bbfd4"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.9"
(many more entries)...
```
Because each project has its own, independent manifest, **each project can have
its own version of the packages it needs**. This is really useful for
stability, and for reproducibility.
### `instantiate` to reproduce
If you have downloaded a directory with a Project.toml created by someone else
(including "past you" or "other computer you"), you can **instantiate** that
environment via:
```julia-repl
julia> # press ] to enter Pkg mode...
(v1.3) pkg> activate path/to/your/project/directory/
Activating environment at `~/path/to/your/project/directory/Project.toml`
(directory) pkg> instantiate
Updating registry at `~/.julia/registries/General`
Updating git-repo `git@github.com:JuliaRegistries/General.git`
Resolving package versions...
Updating `~/code/RePsychLing/path/to/your/project/directory/Project.toml`
[2913bbd2] + StatsBase v0.32.1
Updating `~/code/RePsychLing/path/to/your/project/directory/Manifest.toml`
[9a962f9c] + DataAPI v1.1.0
[864edb3b] + DataStructures v0.17.9
[e1d29d7a] + Missings v0.4.3
[bac558e1] + OrderedCollections v1.1.0
[a2af1166] + SortingAlgorithms v0.3.1
[2913bbd2] + StatsBase v0.32.1
[2a0f44e3] + Base64
[8ba89e20] + Distributed
[b77e0a4c] + InteractiveUtils
[8f399da3] + Libdl
[37e2e46d] + LinearAlgebra
[56ddb016] + Logging
[d6f4376e] + Markdown
[de0858da] + Printf
[9a3f8284] + Random
[9e88b42a] + Serialization
[6462fe0b] + Sockets
[2f01184e] + SparseArrays
[10745b16] + Statistics
[8dfed614] + Test
[4ec0a83e] + Unicode
```
This effectively `add`s every package in the Project.toml. (It also does some
fancy version resolution to find a set of versions that are all mutually
compatible).
If you also have a Manifest.toml for this project, then `instantiate` will
install the **exact versions** of all the packages listed in the Manfiest. This
means that you have a perfectly portable version of your project.
### Other Pkg operations
* `rm` to remove packages
* `up` to update all packges to latest versions
* `up PkgName` to update `PkgName` to the latest (compatible) version.
* `resolve` to try to find a set of compatible versions
### adding specific versions of packages
* `add PkgName@a.b.c` add version `a.b.c` of package (or a compatible version).
* `add PkgName#master` add the master branch (usually pre-release) version of a
package.
* `add https://www.github.com/user/somepackage.jl` add an un-released package
from a git URL.
NOTE: version information is automatically recorded in Manifest.toml, but
**not** in Project.toml.
### compatibility in Project.toml
You can add compatibility restrictions in Project.toml
but this is generally not necessary for one-off projects (like analysis scripts
etc.) which are not intended to wide re-use (unlike a "real" package):
```
[deps]
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
[compat]
StatsBase = "0.32"
```
The rules of which exact versions are compatible are somewhat complex, and are
documented here. The resolver obeys "semantic versioning" or "semver", if that
means anything to you, with the added rule that pre-1.0, a minor version counts
as a breaking ("major") release, so by this rule 1.0, 0.32, 0.31 are all
considered **incompatible**.