|
| 1 | +# Extra material about isolated environments |
| 2 | + |
| 3 | +## Workflow ``venv`` |
| 4 | + |
| 5 | +1. Start from a Python version you would like to use (load the module): |
| 6 | + - This step are different at different clusters since the naming is different |
| 7 | + |
| 8 | +2. Load the Python module you will be using, as well as any site-installed package modules (requires the ``--system-site-packages`` option later) |
| 9 | + - ``module load <python module>`` |
| 10 | + |
| 11 | +The next points will be the same for all clusters |
| 12 | + |
| 13 | +3. Create the isolated environment with something like ``python -m venv <name-of-environment>`` |
| 14 | + - use the ``--system-site-packages`` to include all "non-base" packages |
| 15 | + - include the full path in the name if you want the environment to be stored other than in the "present working directory". |
| 16 | + |
| 17 | +4. Activate the environment with ``source <path to virtual environment>/bin activate`` |
| 18 | + |
| 19 | +:::{note} |
| 20 | + |
| 21 | + - ``source`` can most often be replaced by ``.``, like in ``. Example/bin/activate``. Note the important <space> after ``.`` |
| 22 | + - For clarity we use the ``source`` style here. |
| 23 | +::: |
| 24 | + |
| 25 | +5. Install (or update) the environment with the packages you need with the ``pip install`` command |
| 26 | + |
| 27 | + - Note that ``--user`` must be omitted: else the package will be installed in the global user folder. |
| 28 | + - 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. |
| 29 | + |
| 30 | +6. Work in the isolated environment |
| 31 | + - When activated you can always continue to add packages! |
| 32 | +7. Deactivate the environment after use with ``deactivate`` |
| 33 | + |
| 34 | +:::{note} |
| 35 | + |
| 36 | + 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! |
| 37 | + ``--system-site-packages`` includes the packages already installed in the loaded python module. |
| 38 | + |
| 39 | + 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: |
| 40 | + |
| 41 | + ```console |
| 42 | + |
| 43 | + $ module load buildtool-easybuild/4.8.0-hpce082752a2 GCC/13.2.0 Python/3.11.5 SciPy-bundle/2023.11 # for NSC |
| 44 | + $ python -m venv --system-site-packages Example |
| 45 | + ``` |
| 46 | +::: |
| 47 | + |
| 48 | +:::{warning} |
| 49 | + |
| 50 | + Draw-backs |
| 51 | + |
| 52 | + - Only works for Python environments |
| 53 | + - Only works with Python versions already installed |
| 54 | +::: |
| 55 | + |
| 56 | + |
| 57 | +## Typical workflow Conda |
| 58 | + |
| 59 | + |
| 60 | +The first 2 steps are cluster dependent and will therefore be slightly different. |
| 61 | + |
| 62 | +1. Make conda available from a software module, like ``ml load conda`` or similar, or use own installation of miniconda or miniforge. |
| 63 | +2. First time |
| 64 | + |
| 65 | + :::{admonition} First time |
| 66 | + :class: dropdown |
| 67 | + |
| 68 | + - 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. |
| 69 | + - Otherwise, the default is ``~/.conda/envs``. |
| 70 | + - Example: |
| 71 | + |
| 72 | + ```console |
| 73 | + |
| 74 | + $ export CONDA_ENVS_PATH="path/to/your/project/(subdir)" |
| 75 | + $ export CONDA_PKG_DIRS="path/to/your/project/(subdir)" |
| 76 | + ``` |
| 77 | + |
| 78 | + ::: |
| 79 | + |
| 80 | +Next steps are the same for all clusters |
| 81 | + |
| 82 | +3. Create the conda environment ``conda create -n <name-of-env>`` |
| 83 | +4. Activate the conda environment by: ``source activate <conda-env-name>`` |
| 84 | + |
| 85 | + - You can define the packages to be installed here already. |
| 86 | + - If you want another Python version, you have to define it here, like: ``conda ... python=3.6.8`` |
| 87 | + |
| 88 | +5. Install the packages with ``conda install ...`` or ``pip install ...`` |
| 89 | +6. Now do your work! |
| 90 | + |
| 91 | + - When activated you can always continue to add packages! |
| 92 | + |
| 93 | +7. Deactivate |
| 94 | + |
| 95 | +:::{prompt} |
| 96 | + :language: bash |
| 97 | + :prompts: (python-36-env) $ |
| 98 | + |
| 99 | + conda deactivate |
| 100 | +::: |
0 commit comments