Skip to content

Commit f9ed222

Browse files
authored
use_isolated_environments.md rearrangement
1 parent 99d9dbe commit f9ed222

1 file changed

Lines changed: 55 additions & 169 deletions

File tree

docs/day2/use_isolated_environments.md

Lines changed: 55 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ HPC cluster| Conda vs venv
112112
Alvis | venv, conda in container
113113
Bianca | conda/latest, venv via wharf
114114
COSMOS | Anaconda3/2024.02-1
115-
Dardel | miniconda3/24.7.1-0-cpeGNU-23.12
115+
Dardel | miniconda3/25.3.1-1-cpeGNU-24.11
116116
Kebnekaise | venv **only**
117117
LUMI | venv, conda-containerize
118118
Pelle | venv, Miniforge3/24.11.3-0
@@ -136,7 +136,6 @@ LUMI | conda-containerize
136136
- [documentation](https://pip.pypa.io/en/stable/cli/pip_list)
137137
:::
138138

139-
140139
### Virtual environment - venv & virtualenv
141140

142141
With this tool you can download and install with ``pip`` from the [PyPI repository](https://pypi.org/)
@@ -193,12 +192,9 @@ With this tool you can download and install with ``pip`` from the [PyPI reposito
193192

194193
- You can use "pip list" on the command line (after loading the python module) to see which packages are available and which versions.
195194
- Some packages may be inherited from the modules you have loaded
196-
- You can do ``pip list --local`` to see what is installed by you in the environment.
197-
- Some IDE:s like Spyder may only find those "local" packages
198195
- To save space, you should load any other Python modules you will need that are system installed before installing your own packages! Remember to choose ones that are compatible with the Python version you picked!
199196
- ``--system-site-packages`` includes the packages already installed in the loaded python module.
200197
- The ``--no-cache-dir"`` option is required to **avoid it from reusing earlier installations from the same user in a different environment**.
201-
- The ``--no-build-isolation`` is to make sure that it uses the loaded modules from the module system when **building any Cython libraries**.
202198
:::
203199

204200
### Conda
@@ -225,81 +221,62 @@ With this tool you can download and install with ``pip`` from the [PyPI reposito
225221
- scilifelab-lts
226222

227223
You reach them all by loading the conda module. You don't have to state the specific channel when using UPPMAX. Otherwise you do with ``conda -c <channel> ...``
228-
224+
:::
229225

230226
:::{warning}
231227

232-
Drawbacks
228+
Drawbacks
233229

234-
- Conda cannot use already install packages from the Python modules and libraries already installed, and hence installs them anyway
235-
- Conda is therefore known for creating **many** *small* files. Your disk space is not only limited in GB, but also in number of files (typically ``300000`` in $HOME).
236-
- Check your disk usage and quota limit
237-
- Do a ``conda clean -a`` once in a while to remove unused and unnecessary files
230+
- Conda cannot use already install packages from the Python modules and libraries already installed, and hence installs them anyway
231+
- Conda is therefore known for creating **many** *small* files. Your disk space is not only limited in GB, but also in number of files (typically ``300000`` in $HOME).
232+
- Check your disk usage and quota limit
233+
- Do a ``conda clean -a`` once in a while to remove unused and unnecessary files
238234
:::
239235

240236
:::{tip}
241237

242238
- The conda environments including many small files are by default stored in ``~/.conda`` folder that is in your $HOME directory with limited storage.
243-
- Move your ``.conda`` directory to your project folder and make a soft link to it from ``$HOME``
244-
- Do the following (``mkdir -p`` ignores error output and will not recreate another folder if it already exists):
245-
- (replace what is inside ``<>`` with relevant path)
246-
247-
- Solution 1
248-
249-
This works nicely if you have several projects. Then you can change these variables according to what you are currently working with.
250-
251-
```bash
239+
- Use the commands
252240

253-
export CONDA_ENVS_PATH="path/to/your/project/(subdir)"
254-
export CONDA_PKG_DIRS="path/to/your/project/(subdir)"
255-
mamba create --prefix=$CONDA_ENVS_PATH/<conda env name>
256-
```
257-
258-
- Solution 2
259-
260-
- This may not be a good idea if you have several projects.
261-
262-
```bash
241+
```bash
242+
export CONDA_ENVS_PATH="path/to/your/project/(subdir)"
243+
export CONDA_PKG_DIRS="path/to/your/project/(subdir)"
244+
mamba create --prefix=$CONDA_ENVS_PATH/<conda env name>
245+
```
263246

264-
$ mkdir -p ~/.conda
265-
$ mv ~/.conda /<path-to-project-folder>/<username>/
266-
$ ln -s /<path-to-project-folder>/<username>/.conda ~/.conda
267-
```
268247
:::
269248

270249
:::{admonition} Example NSC
271250

272-
```console
251+
```console
273252

274-
module load Miniforge/24.7.1-2-hpc1
275-
export CONDA_PKG_DIRS=/proj/spring-courses-naiss/users/$USER
276-
export CONDA_ENVS_PATH=/proj/spring-courses-naiss/users/$USER
277-
mamba create --prefix=$CONDA_ENVS_PATH/numpy-proj-39 python=3.9.5 -c conda-forge
278-
mamba activate nsc-example
279-
# A prompt "(/path-to/nsc-example/)" should show up
280-
# double-check we are using python from the Conda environment!
281-
which python # should point to the conda environment!
282-
python -V # should give python version 3.9.5
283-
mamba install numpy
284-
```
253+
module load Miniforge/24.7.1-2-hpc1
254+
export CONDA_PKG_DIRS=/proj/spring-courses-naiss/users/$USER
255+
export CONDA_ENVS_PATH=/proj/spring-courses-naiss/users/$USER
256+
mamba create --prefix=$CONDA_ENVS_PATH/numpy-proj-39 python=3.9.5 -c conda-forge
257+
mamba activate nsc-example
258+
# A prompt "(/path-to/nsc-example/)" should show up
259+
# double-check we are using python from the Conda environment!
260+
which python # should point to the conda environment!
261+
python -V # should give python version 3.9.5
262+
conda install numpy
263+
```
285264

286-
```python
265+
```python
287266

288-
>>> import numpy
289-
```
267+
>>> import numpy
268+
```
290269
:::
291270

292-
:::{admonition} Comments
293-
:class: dropdown
294-
295-
- When pinning with Conda, use single ``=`` instead of double (as used by pip)
271+
- Note, hen pinning with Conda, use single ``=`` instead of double (as used by pip)
296272
:::
297273

298274
:::{admonition} Conda base env
299275
:class: dropdown
300276

301-
- When conda is loaded you will by default be in the base environment, which works in the same way as other conda environments.
302-
- It includes a Python installation and some core system libraries and dependencies of Conda. It is a “best practice” to avoid installing additional packages into your base software environment.
277+
- When conda is loaded you will by default be in the *base environment*, which works in the same way as other conda environments.
278+
- It includes a Python installation and some core system libraries and dependencies of Conda.
279+
- It is a “best practice” to avoid installing additional packages into your base software environment.
303280

304281
:::{admonition} Conda cheat sheet
305282

@@ -321,29 +298,28 @@ With this tool you can download and install with ``pip`` from the [PyPI reposito
321298
- [what-is-the-difference-with-conda-mamba-poetry-pip](https://pixi.sh/latest/misc/FAQ/#what-is-the-difference-with-conda-mamba-poetry-pip)
322299
:::
323300

324-
:::{admonition} What to do when a problem arises?
325-
:class: dropdown
326-
327-
- If you experience unexpected problems with the conda provided by the module system on Rackham or anaconda3 on Dardel, you can easily install your own and maintain it yourself.
328-
- Read more at [Pavlin Mitev's page about conda on Rackham/Dardel](https://hackmd.io/@pmitev/conda_on_Rackham) and change paths to relevant one for your system.
329-
- Or [Conda - "best practices" - UPPMAX](https://hackmd.io/@pmitev/module_conda_Rackham)
330-
:::
331-
332301
## Install from file
333302

334-
- All centers has had different approaches in what is included in the module system and not.
335-
- Therefore the solution to complete the necessary packages needed for the course lessons, different approaches has to be made.
336-
- This is left as exercise for you, see Exercise 4 and 5.
303+
- This is handy when you want to move your Python environment somewhere else.
304+
- Also, when giving your code to someone else (in research group or to a community).
305+
- Good for reproducibility
337306

338-
### venv
307+
:::{admonition} Principle for both ``venv`` & ``conda``
308+
1. Activate the environment and make sure it works
309+
2. Save a environment file
310+
3. Distribute the file
311+
4. Receiver installs the environment from the file
312+
:::
339313

314+
### venv
340315

341316
Make a requirements file:
342317

343318
```console
344319

345-
pip freeze --local > requirements.txt
320+
pip freeze --local > requirements.txt
346321
```
322+
347323
:::{admonition} How does it look like?
348324
:class: dropdown
349325

@@ -360,6 +336,7 @@ Make a requirements file:
360336
python-dateutil==2.9.0.post0
361337
six==1.17.0
362338
```
339+
:::
363340

364341
Install packages from a file
365342

@@ -440,6 +417,10 @@ Create an environment from a file. Do this on another computer or rename.
440417

441418
## Exercises
442419

420+
- All centers has had different approaches in what is included in the module system and not.
421+
- Therefore the solution to complete the necessary packages needed for the course lessons, different approaches has to be made.
422+
- This is left as exercise for you, see Exercise 4 and 5.
423+
443424
:::{challenge} Exercise 0: Make a decision between ``venv`` or ``conda``.
444425

445426
- We recommend `conda` for LUNARC.
@@ -1028,9 +1009,7 @@ Breakout room according to grouping
10281009
```
10291010
:::
10301011

1031-
Summary
1032-
-------
1033-
1012+
## Summary
10341013

10351014
:::{keypoints}
10361015

@@ -1045,6 +1024,11 @@ Summary
10451024
- install in project folder due to many files.
10461025
:::
10471026

1027+
Summary of the workflows:
1028+
1029+
- [Venv](../extra/extra_isolated.md#)
1030+
- [Conda](../extra/extra_isolated.md#
1031+
10481032
:::{admonition} Documentation at the centres
10491033
:class: dropdown
10501034

@@ -1076,105 +1060,7 @@ Summary
10761060
- https://docs.lumi-supercomputer.eu/software/installing/container-wrapper/#examples-of-using-the-lumi-container-wrapper
10771061
:::
10781062

1079-
## Summary
1080-
1081-
### Workflow ``venv``
1082-
1083-
1. Start from a Python version you would like to use (load the module):
1084-
- This step are different at different clusters since the naming is different
1085-
1086-
2. Load the Python module you will be using, as well as any site-installed package modules (requires the ``--system-site-packages`` option later)
1087-
- ``module load <python module>``
1088-
1089-
The next points will be the same for all clusters
1090-
1091-
3. Create the isolated environment with something like ``python -m venv <name-of-environment>``
1092-
- use the ``--system-site-packages`` to include all "non-base" packages
1093-
- include the full path in the name if you want the environment to be stored other than in the "present working directory".
1094-
1095-
4. Activate the environment with ``source <path to virtual environment>/bin activate``
1096-
1097-
:::{note}
1098-
1099-
- ``source`` can most often be replaced by ``.``, like in ``. Example/bin/activate``. Note the important <space> after ``.``
1100-
- For clarity we use the ``source`` style here.
1101-
:::
1102-
1103-
5. Install (or update) the environment with the packages you need with the ``pip install`` command
1104-
1105-
- Note that ``--user`` must be omitted: else the package will be installed in the global user folder.
1106-
- The ``--no-cache-dir"`` option is required to avoid it from reusing earlier installations from the same user in a different environment. The ``--no-build-isolation`` is to make sure that it uses the loaded modules from the module system when building any Cython libraries.
11071063

1108-
6. Work in the isolated environment
1109-
- When activated you can always continue to add packages!
1110-
7. Deactivate the environment after use with ``deactivate``
1111-
1112-
:::{note}
1113-
1114-
To save space, you should load any other Python modules you will need that are system installed before installing your own packages! Remember to choose ones that are compatible with the Python version you picked!
1115-
``--system-site-packages`` includes the packages already installed in the loaded python module.
1116-
1117-
At HPC2N, NSC and LUNARC, you often have to load SciPy-bundle. This is how you on Tetralith (NSC) could create a venv (Example) with a SciPy-bundle included which is compatible with Python/3.11.5:
1118-
1119-
```console
1120-
1121-
$ module load buildtool-easybuild/4.8.0-hpce082752a2 GCC/13.2.0 Python/3.11.5 SciPy-bundle/2023.11 # for NSC
1122-
$ python -m venv --system-site-packages Example
1123-
```
1124-
:::
1125-
1126-
:::{warning}
1127-
1128-
Draw-backs
1129-
1130-
- Only works for Python environments
1131-
- Only works with Python versions already installed
1132-
:::
1133-
1134-
Typical workflow Conda
1135-
......................
1136-
1137-
The first 2 steps are cluster dependent and will therefore be slightly different.
1138-
1139-
1. Make conda available from a software module, like ``ml load conda`` or similar, or use own installation of miniconda or miniforge.
1140-
2. First time
1141-
1142-
:::{admonition} First time
1143-
:class: dropdown
1144-
1145-
- The variables CONDA_ENVS_PATH and CONDA_PKG_DIRS contains the location of your environments. Set it to your project's environments folder, if you have one, instead of the $HOME folder.
1146-
- Otherwise, the default is ``~/.conda/envs``.
1147-
- Example:
1148-
1149-
```console
1150-
1151-
$ export CONDA_ENVS_PATH="path/to/your/project/(subdir)"
1152-
$ export CONDA_PKG_DIRS="path/to/your/project/(subdir)"
1153-
```
1154-
1155-
:::
1156-
1157-
Next steps are the same for all clusters
1158-
1159-
3. Create the conda environment ``conda create -n <name-of-env>``
1160-
4. Activate the conda environment by: ``source activate <conda-env-name>``
1161-
1162-
- You can define the packages to be installed here already.
1163-
- If you want another Python version, you have to define it here, like: ``conda ... python=3.6.8``
1164-
1165-
5. Install the packages with ``conda install ...`` or ``pip install ...``
1166-
6. Now do your work!
1167-
1168-
- When activated you can always continue to add packages!
1169-
1170-
7. Deactivate
1171-
1172-
:::{prompt}
1173-
:language: bash
1174-
:prompts: (python-36-env) $
1175-
1176-
conda deactivate
1177-
:::
11781064

11791065
:::{seealso}
11801066

0 commit comments

Comments
 (0)