Skip to content

Commit 5d4bf15

Browse files
committed
Updated dependencies chapter for 2023 course
1 parent b17afe9 commit 5d4bf15

File tree

1 file changed

+78
-67
lines changed

1 file changed

+78
-67
lines changed

content/dependencies.rst

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ PyPI (The Python Package Index) and (Ana)conda
5555
management tools.
5656

5757
- When you run ``pip install`` you typically install from `PyPI
58-
<https://pypi.org/>`__ but one can also ``pip install`` from a GitHub
58+
<https://pypi.org/>`__ but you can also ``pip install`` from a GitHub
5959
repository and similar.
6060

6161
- When you run ``conda install`` you typically install from `Anaconda Cloud
62-
<https://anaconda.org/>`__ but there are many community-driven conda channels
63-
and you can create your own.
62+
<https://anaconda.org/>`__ where there are conda channels maintained
63+
by Anaconda Inc. and by various communities.
6464

6565

6666
Why are there two ecosystems?
@@ -71,7 +71,7 @@ Why are there two ecosystems?
7171
- **Installation tool:** ``pip``
7272
- **Summary:** PyPI is traditionally used for Python-only packages or
7373
for Python interfaces to external libraries. There are also packages
74-
with bundled external libraries (such as numpy).
74+
that have bundled external libraries (such as numpy).
7575
- **Amount of packages:** Huge number. Old versions are supported for
7676
a long time.
7777
- **How libraries are handled:** If your code depends on external
@@ -92,8 +92,8 @@ Why are there two ecosystems?
9292
and tools needed by the Python packages. Most scientific software written
9393
in Python uses external libraries to speed up calculations and installing
9494
these libraries can often become complicated without conda.
95-
- **Amount of packages:** Most popular packages are provided. Other packages
96-
can be installed via pip.
95+
- **Amount of packages:** Curated list of packages in defaults-channel, huge
96+
number in community managed channels. Other packages can be installed via pip.
9797
- **How libraries are handled:** Required libraries are installed as separate
9898
conda packages.
9999
- **Pros:**
@@ -102,21 +102,35 @@ Why are there two ecosystems?
102102
- **Cons:**
103103
- Package creation is harder
104104

105-
.. admonition:: Anaconda vs. miniconda vs. conda vs. mamba vs. Anaconda Cloud vs. conda-forge
105+
.. admonition:: Anaconda vs. miniconda vs. conda vs. mamba vs. Anaconda Cloud vs. conda-forge vs. miniforge
106106
:class: dropdown
107107

108-
- `Anaconda <https://www.anaconda.com/>`__ - a distribution of conda packages
109-
made by Anaconda Inc.. It is free for academic and non-commercial use.
110-
- `Miniconda <https://conda.io/miniconda.html>`__ - a minimal installer for conda.
111-
- `conda <https://conda.io/>`__ - a package and environment management system
112-
used by Anaconda. It is an open source project maintained by Anaconda Inc..
113-
- `mamba <https://mamba.readthedocs.io/en/latest/index.html>`__ - a drop in
114-
replacement for conda that does installations faster.
108+
Package sources:
109+
115110
- `Anaconda Cloud <https://anaconda.org/>`__ - a package cloud maintained by
116111
Anaconda Inc. It is a free repository that houses conda package channels.
117112
- `Conda-forge <https://conda-forge.org/>`__ - the largest open source
118113
community channel.
119114

115+
Package managers:
116+
117+
- `conda <https://conda.io/>`__ - a package and environment management system
118+
used by Anaconda. It is an open source project maintained by Anaconda Inc..
119+
- `mamba <https://mamba.readthedocs.io/en/latest/index.html>`__ - a drop in
120+
replacement for conda that does installations faster.
121+
122+
Package manager deployments:
123+
124+
- `Anaconda <https://www.anaconda.com/>`__ - a distribution of conda packages
125+
made by Anaconda Inc.. It is free for academic and non-commercial use.
126+
- `Miniconda <https://conda.io/miniconda.html>`__ - a minimal installer that
127+
has conda and uses
128+
`default channels <https://docs.anaconda.com/free/anaconda/reference/default-repositories/#active-default-channels>`__
129+
by default.
130+
- `Miniforge <https://github.com/conda-forge/miniforge>`__ - Miniconda replacement
131+
that uses conda-forge as the default channel. Contains mamba as well.
132+
133+
120134
In the packaging episode we will meet PyPI and Anaconda again and practice how
121135
to share Python packages.
122136

@@ -128,7 +142,7 @@ An **isolated environment** allows installing packages without
128142
affecting the rest of your operating system or any other projects.
129143
Isolated environments solve a couple of problems:
130144

131-
- You can install specific, also older, versions of packages into them.
145+
- You can install specific versions of packages into them.
132146

133147
- You can create one environment for each project and you won't encounter any
134148
problems if the two projects require different versions of packages.
@@ -152,9 +166,9 @@ Exercises 2
152166
few weeks) and you give her a Python code for analyzing and plotting your
153167
favorite data. The thing is that your Python code has been developed by
154168
another Master Student (from last year) and requires a older version of
155-
Numpy (1.18.1) and Matplotlib (3.1.3) (otherwise the code fails). The code
169+
Numpy (1.24.3) and Matplotlib (3.7.2) (otherwise the code fails). The code
156170
could probably work with a recent version of Python but has been validated with
157-
Python 3.7 only. Having no idea what the code does, she decides that the best
171+
Python 3.10 only. Having no idea what the code does, she decides that the best
158172
approach is to **create an isolated environment** with the same dependencies
159173
that were used previously. This will give her a baseline for future upgrade and
160174
developments.
@@ -163,23 +177,31 @@ Exercises 2
163177

164178
1. Create a conda environment::
165179

166-
$ conda create --name python37-env python=3.7 numpy=1.18.1 matplotlib=3.1.3
180+
$ conda create --name python310-env python=3.10 numpy=1.24.3 matplotlib=3.7.2
167181

168182
Conda environments can also be managed (create, update, delete) from the
169183
**anaconda-navigator**. Check out the corresponding documentation `here
170184
<https://docs.anaconda.com/navigator/getting-started/#navigator-managing-environments>`_.
171185

172186
2. Activate the environment::
173187

174-
$ conda activate python37-env
188+
$ conda activate python310-env
175189

176190
.. callout:: conda activate versus source activate
177191

178-
If you do not have a recent version of Anaconda or anaconda has not been
179-
setup properly, you may encounter an error. With older version of anaconda,
180-
you can try::
192+
``conda activate`` will only work if you have run ``conda init``
193+
in the past. Running ``conda init`` will make loading environments
194+
easier as you will always have a conda environment loaded.
195+
196+
However, this can also cause problems as programs in the
197+
main environment will be constantly loaded and they might be used
198+
even when they're not supposed to be used. A common example is
199+
not having ``pip`` installed in a conda environment which results
200+
``pip`` from main environment to be used instead.
181201

182-
$ source activate python37-env
202+
You can always try::
203+
204+
$ source activate python310-env
183205

184206
3. Open a Python console and check that you have effectively the
185207
right version for each package:
@@ -204,26 +226,11 @@ Exercises 2
204226
$ conda deactivate
205227

206228
5. Check Numpy and Matplotlib versions in the default environment to make
207-
sure they are different from **python37-env**.
229+
sure they are different from **python310-env**.
208230

209231
There is no need to specify the conda environment when using deactivate. It
210232
deactivates the current environment.
211233

212-
.. callout:: Remark
213-
214-
- Sometimes the package version you would need does not seem to be
215-
available. You may have to select another `conda channel
216-
<https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/channels.html>`_
217-
for instance `conda-forge <https://conda-forge.org/>`_. Channels can then
218-
be indicated when installing a package::
219-
220-
$ conda install -c conda-forge matplotlib=3.1.3
221-
222-
- We will see below that rather than specifying the list of dependencies as
223-
argument of ``conda create``, it is recommended to record dependencies in
224-
a file.
225-
226-
227234

228235
Exercises 3
229236
-----------
@@ -249,10 +256,10 @@ Exercises 3
249256

250257
- **Windows**: most likely you can find it in the ``Scripts`` folder.
251258

252-
3. Install Numpy 1.18.1 and Matplotlib 3.1.3 into the virtual environment::
259+
3. Install Numpy 1.24.3 and Matplotlib 3.7.2 into the virtual environment::
253260

254-
$ pip install numpy==1.18.1
255-
$ pip install matplotlib==3.1.3
261+
$ pip install numpy==1.24.3
262+
$ pip install matplotlib==3.7.2
256263

257264
4. Deactivate it::
258265

@@ -271,21 +278,6 @@ The reason for this is that the installer does not know what commands
271278
you ran in the past. It only knows the state of your environment and what
272279
you're currently telling it to install.
273280

274-
For example, check what version of ``scipy`` you'll get if you run
275-
276-
.. code-block:: console
277-
278-
$ pip install scipy
279-
280-
or
281-
282-
.. code-block:: console
283-
284-
$ conda install scipy
285-
286-
Depending on your environment you can get ``scipy`` with versions from
287-
``1.6.2`` with no numpy upgrade to ``1.9.3`` with automatic numpy upgrade.
288-
289281
These kinds of problems can be mitigated by recording dependencies in an
290282
``environment.yml`` or ``requirements.txt``.
291283

@@ -324,16 +316,32 @@ is usually a delay between doing research and that research being published.
324316
During this time the dependencies might update and reviewers or interested
325317
researchers might not be able to replicate your results or run your code.
326318

319+
.. callout:: Conda channels
320+
321+
- Sometimes the package version you would need does not seem to be
322+
available. You may have to select another `conda channel
323+
<https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/channels.html>`__.
324+
325+
Most popular channels are
326+
`defaults <https://docs.anaconda.com/free/anaconda/reference/default-repositories/#active-default-channels>`__,
327+
which is managed by
328+
Anaconda Inc. and `conda-forge <https://conda-forge.org/>`__,
329+
which is managed by the open source community. These two channels are
330+
mutually incompatible.
331+
332+
Channel priority goes from top to bottom.
333+
334+
327335
Here are the two files again, but this time with versions pinned:
328336

329337
``requirements.txt`` with versions:
330338

331339
.. code-block:: none
332340
333-
numpy==1.18.1
334-
matplotlib==3.1.3
335-
pandas==1.1.2
336-
scipy==1.6.2
341+
numpy==1.24.3
342+
matplotlib==3.7.2
343+
pandas==2.0.3
344+
scipy==1.10.1
337345
338346
``environments.yml`` with versions:
339347

@@ -343,11 +351,11 @@ Here are the two files again, but this time with versions pinned:
343351
channels:
344352
- defaults
345353
dependencies:
346-
- python=3.7
347-
- numpy=1.18.1
348-
- matplotlib=3.1.3
349-
- pandas=1.1.2
350-
- scipy=1.6.2
354+
- python=3.10
355+
- numpy=1.24.3
356+
- matplotlib=3.7.2
357+
- pandas=2.0.3
358+
- scipy=1.10.1
351359
352360
- Conda can also read and write ``requirements.txt``.
353361
- ``requirements.txt`` can also refer to packages on Github.
@@ -475,8 +483,11 @@ Other tools for dependency management:
475483
- `micropipenv <https://github.com/thoth-station/micropipenv>`__: lightweight tool to "rule them all"
476484
- `mamba <https://mamba.readthedocs.io/en/latest/index.html>`__: a drop in replacement for
477485
conda that does installations faster.
478-
- `miniforge & mambaforge <https://github.com/conda-forge/miniforge>`__: Miniconda alternatives with
486+
- `miniforge <https://github.com/conda-forge/miniforge>`__: Miniconda alternative with
479487
conda-forge as the default channel and optionally mamba as the default installer.
488+
- `micromamba <https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html>`__:
489+
tiny version of Mamba as a static C++ executable. Does not need base environment or
490+
Python for installing an environment.
480491

481492
Other resources:
482493

0 commit comments

Comments
 (0)