Skip to content

Commit ec0fc2e

Browse files
stable link dependencies chapter
1 parent 54d49fa commit ec0fc2e

File tree

1 file changed

+152
-151
lines changed

1 file changed

+152
-151
lines changed

book/061_dependencies.ipynb

Lines changed: 152 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,154 @@
11
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "97a9ac71-a121-4b3e-bd73-a9721222d5ac",
6-
"metadata": {},
7-
"source": [
8-
"---\n",
9-
"title: Dependencies\n",
10-
"toc: true\n",
11-
"---"
12-
]
13-
},
14-
{
15-
"cell_type": "markdown",
16-
"id": "c79acc5e-0543-4994-b09a-1af21a509cd2",
17-
"metadata": {},
18-
"source": [
19-
"In contrast to our own code that we can import or to some built-in modules such as `pathlib` that we can import directly after installing python, there are third-party libraries that we want to use but need to be first installed, such as `numpy` and `pandas`.\n",
20-
"This libraries are also called \"packages\".\n",
21-
"\n",
22-
"There are several repositories (also called \"indexes\") where people publish python packages and from which we can download them. The official one is [PyPI](https://pypi.org) (Python Package Index).\n",
23-
"\n",
24-
"These are open-source libraries maintained mostly by voluntary contributions of people of the community – and you can also contribute to them!\n",
25-
"\n",
26-
"These packages are being actively developed and thus in permanent evolution.\n",
27-
"In order to avoid compatibility problems and to ensure reproducibility, we might want to specify the exact versions of the packages we use.\n",
28-
"It turns out that is not an easy problem and the reason why need a **package manager**.\n",
29-
"\n",
30-
"The good news is that `uv` does that for us too."
31-
]
32-
},
33-
{
34-
"cell_type": "markdown",
35-
"id": "127f7c9d-dccd-48bd-bc99-e1e7de44cb6c",
36-
"metadata": {},
37-
"source": [
38-
"## Managing dependencies"
39-
]
40-
},
41-
{
42-
"cell_type": "markdown",
43-
"id": "2dce4970-2fa6-43ea-958b-1ae6a379dc94",
44-
"metadata": {},
45-
"source": [
46-
"The standard to specify dependencies (and other things) in python projects is to use a file called [`pyproject.toml`](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) that looks like this:\n",
47-
"\n",
48-
"```yaml\n",
49-
"[project]\n",
50-
"name = \"example_package_name\"\n",
51-
"version = \"0.0.1\"\n",
52-
"authors = [\n",
53-
" { name=\"Example Author\", email=\"author@example.com\" },\n",
54-
"]\n",
55-
"description = \"A small example package\"\n",
56-
"readme = \"README.md\"\n",
57-
"requires-python = \">=3.10,<3.12\"\n",
58-
"dependencies = [\n",
59-
" \"numpy==1.2.0\",\n",
60-
" \"pandas<2.0.0\",\n",
61-
" \"scikit-learn>=1.0,<2.0\"\n",
62-
"]\n",
63-
"```\n",
64-
"\n",
65-
"::: {.callout-note}\n",
66-
"Take a look at the `pyproject.toml` file in you project, we already have one that `uv` automatically created for us!\n",
67-
":::\n",
68-
"\n",
69-
"The \"dependencies\" section is the one we care about here.\n",
70-
"There we list all the third-party libraries that our project requires.\n",
71-
"Notice that we can specify both exact versions (with \"==\") as well as constraints (with \"<,>,>=,<=\").\n",
72-
"\n",
73-
"In general we don't want to deal manually with those aspects – that's the job of a package manager."
74-
]
75-
},
76-
{
77-
"cell_type": "markdown",
78-
"id": "00421264-b754-49af-a789-dcdf3ebe6325",
79-
"metadata": {},
80-
"source": [
81-
"::: {.callout-note}\n",
82-
"The packages usually follow a so-called \"Semantic versioning\" convention on how to name the versions of the package to avoid compatibility problems.\n",
83-
"In short: Given a version number MAJOR.MINOR.PATCH.\n",
84-
"\n",
85-
"> MAJOR version when you make incompatible API changes \n",
86-
"MINOR version when you add functionality in a backward compatible manner \n",
87-
"PATCH version when you make backward compatible bug fixes \n",
88-
"\n",
89-
"Read more about that [here](https://semver.org).\n",
90-
":::"
91-
]
92-
},
93-
{
94-
"cell_type": "markdown",
95-
"id": "f9e9ab15-34ff-4b43-bf21-b865aaa660ac",
96-
"metadata": {},
97-
"source": [
98-
"We will use here the package manager [uv](https://docs.astral.sh/uv).\n",
99-
"You can read the docs for more details, which I strongly recommend since the documentation is pretty good and constantly being updated.\n",
100-
"To add a dependency we type:\n",
101-
"\n",
102-
"```bash\n",
103-
"uv add PACKAGE-NAME\n",
104-
"```\n",
105-
"For example:\n",
106-
"```bash\n",
107-
"uv add numpy\n",
108-
"```\n",
109-
"\n",
110-
"That will add the dependency to our list as well as updating the \"lock\" file.\n",
111-
"Read more about this workflow and managing dependencies [here](https://docs.astral.sh/uv/guides/projects/#working-on-projects).\n",
112-
"\n",
113-
"The point is that after installing the dependency we can just import it like any other module.\n",
114-
"\n",
115-
"To remove a package from the dependencies of our project we do:\n",
116-
"```bash\n",
117-
"uv remove PACKAGE-NAME\n",
118-
"```\n",
119-
"For example:\n",
120-
"```bash\n",
121-
"uv remove numpy\n",
122-
"```"
123-
]
124-
},
125-
{
126-
"cell_type": "markdown",
127-
"id": "64fc1a40-04fe-40da-8875-04e4aa5d4d46",
128-
"metadata": {},
129-
"source": []
130-
}
131-
],
132-
"metadata": {
133-
"kernelspec": {
134-
"display_name": "Python 3 (ipykernel)",
135-
"language": "python",
136-
"name": "python3"
137-
},
138-
"language_info": {
139-
"codemirror_mode": {
140-
"name": "ipython",
141-
"version": 3
142-
},
143-
"file_extension": ".py",
144-
"mimetype": "text/x-python",
145-
"name": "python",
146-
"nbconvert_exporter": "python",
147-
"pygments_lexer": "ipython3",
148-
"version": "3.13.3"
149-
}
150-
},
151-
"nbformat": 4,
152-
"nbformat_minor": 5
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "97a9ac71-a121-4b3e-bd73-a9721222d5ac",
6+
"metadata": {},
7+
"source": [
8+
"---\n",
9+
"title: Dependencies\n",
10+
"toc: true\n",
11+
"output-file: dependencies.html\n",
12+
"---"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"id": "c79acc5e-0543-4994-b09a-1af21a509cd2",
18+
"metadata": {},
19+
"source": [
20+
"In contrast to our own code that we can import or to some built-in modules such as `pathlib` that we can import directly after installing python, there are third-party libraries that we want to use but need to be first installed, such as `numpy` and `pandas`.\n",
21+
"This libraries are also called \"packages\".\n",
22+
"\n",
23+
"There are several repositories (also called \"indexes\") where people publish python packages and from which we can download them. The official one is [PyPI](https://pypi.org) (Python Package Index).\n",
24+
"\n",
25+
"These are open-source libraries maintained mostly by voluntary contributions of people of the community – and you can also contribute to them!\n",
26+
"\n",
27+
"These packages are being actively developed and thus in permanent evolution.\n",
28+
"In order to avoid compatibility problems and to ensure reproducibility, we might want to specify the exact versions of the packages we use.\n",
29+
"It turns out that is not an easy problem and the reason why need a **package manager**.\n",
30+
"\n",
31+
"The good news is that `uv` does that for us too."
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"id": "127f7c9d-dccd-48bd-bc99-e1e7de44cb6c",
37+
"metadata": {},
38+
"source": [
39+
"## Managing dependencies"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"id": "2dce4970-2fa6-43ea-958b-1ae6a379dc94",
45+
"metadata": {},
46+
"source": [
47+
"The standard to specify dependencies (and other things) in python projects is to use a file called [`pyproject.toml`](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) that looks like this:\n",
48+
"\n",
49+
"```yaml\n",
50+
"[project]\n",
51+
"name = \"example_package_name\"\n",
52+
"version = \"0.0.1\"\n",
53+
"authors = [\n",
54+
" { name=\"Example Author\", email=\"author@example.com\" },\n",
55+
"]\n",
56+
"description = \"A small example package\"\n",
57+
"readme = \"README.md\"\n",
58+
"requires-python = \">=3.10,<3.12\"\n",
59+
"dependencies = [\n",
60+
" \"numpy==1.2.0\",\n",
61+
" \"pandas<2.0.0\",\n",
62+
" \"scikit-learn>=1.0,<2.0\"\n",
63+
"]\n",
64+
"```\n",
65+
"\n",
66+
"::: {.callout-note}\n",
67+
"Take a look at the `pyproject.toml` file in you project, we already have one that `uv` automatically created for us!\n",
68+
":::\n",
69+
"\n",
70+
"The \"dependencies\" section is the one we care about here.\n",
71+
"There we list all the third-party libraries that our project requires.\n",
72+
"Notice that we can specify both exact versions (with \"==\") as well as constraints (with \"<,>,>=,<=\").\n",
73+
"\n",
74+
"In general we don't want to deal manually with those aspects – that's the job of a package manager."
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"id": "00421264-b754-49af-a789-dcdf3ebe6325",
80+
"metadata": {},
81+
"source": [
82+
"::: {.callout-note}\n",
83+
"The packages usually follow a so-called \"Semantic versioning\" convention on how to name the versions of the package to avoid compatibility problems.\n",
84+
"In short: Given a version number MAJOR.MINOR.PATCH.\n",
85+
"\n",
86+
"> MAJOR version when you make incompatible API changes \n",
87+
"MINOR version when you add functionality in a backward compatible manner \n",
88+
"PATCH version when you make backward compatible bug fixes \n",
89+
"\n",
90+
"Read more about that [here](https://semver.org).\n",
91+
":::"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "f9e9ab15-34ff-4b43-bf21-b865aaa660ac",
97+
"metadata": {},
98+
"source": [
99+
"We will use here the package manager [uv](https://docs.astral.sh/uv).\n",
100+
"You can read the docs for more details, which I strongly recommend since the documentation is pretty good and constantly being updated.\n",
101+
"To add a dependency we type:\n",
102+
"\n",
103+
"```bash\n",
104+
"uv add PACKAGE-NAME\n",
105+
"```\n",
106+
"For example:\n",
107+
"```bash\n",
108+
"uv add numpy\n",
109+
"```\n",
110+
"\n",
111+
"That will add the dependency to our list as well as updating the \"lock\" file.\n",
112+
"Read more about this workflow and managing dependencies [here](https://docs.astral.sh/uv/guides/projects/#working-on-projects).\n",
113+
"\n",
114+
"The point is that after installing the dependency we can just import it like any other module.\n",
115+
"\n",
116+
"To remove a package from the dependencies of our project we do:\n",
117+
"```bash\n",
118+
"uv remove PACKAGE-NAME\n",
119+
"```\n",
120+
"For example:\n",
121+
"```bash\n",
122+
"uv remove numpy\n",
123+
"```"
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"id": "64fc1a40-04fe-40da-8875-04e4aa5d4d46",
129+
"metadata": {},
130+
"source": []
131+
}
132+
],
133+
"metadata": {
134+
"kernelspec": {
135+
"display_name": "Python 3 (ipykernel)",
136+
"language": "python",
137+
"name": "python3"
138+
},
139+
"language_info": {
140+
"codemirror_mode": {
141+
"name": "ipython",
142+
"version": 3
143+
},
144+
"file_extension": ".py",
145+
"mimetype": "text/x-python",
146+
"name": "python",
147+
"nbconvert_exporter": "python",
148+
"pygments_lexer": "ipython3",
149+
"version": "3.13.3"
150+
}
151+
},
152+
"nbformat": 4,
153+
"nbformat_minor": 5
153154
}

0 commit comments

Comments
 (0)