Isolated environments, extra exercises

Exercise 2: work with Example-gpu

  • Rehearse creating a Python virtual environment from a step-by-step instruction

This exercise if for UPPMAX users only.

In this exercise, we create another environment Example-gpu in the same step-by-step fashion as done in exercise 1:

flowchart TD
  load_modules[1.Load modules]
  create[2.Create]
  activate[3.Activate]
  install_libraries[4.Install Python libraries]
  check[5.Check installed Python libraries]
  use[6.Use]
  deactivate[7.Deactivate]

  load_modules --> create
  create --> activate
  activate --> install_libraries
  install_libraries --> check
  check --> use
  use --> deactivate
  deactivate --> activate

This virtual environment called Example-gpu is used for examples where the use of GPUs is demonstrated, by using the numba and PyTorch Python packages.

Because the structure is the same as Exercise 1, see exercise 1 for details.

Exercise 2.1: load the modules needed

Here we need an older Python module, as that is what available on the Snowy computer cluster:

module load python/3.9.5

Exercise 2.2: create the virtual environment

python -m venv --system-site-packages /proj/r-py-jl/<user>/python/Example-gpu

where [username] is your UPPMAX username, for example python -m venv --system-site-packages /proj/r-py-jl/sven/python/Example-gpu.

Exercise 2.3: activate the virtual environment

source /proj/r-py-jl/<user>/python/Example-gpu/bin/activate

where [username] is your UPPMAX username, for example source /proj/r-py-jl/sven/python/Example-gpu/bin/activate.

Exercise 2.4: install Python packages

Installing these packages takes around 6 minutes.

pip install --upgrade numpy scipy numba torch

Exercise 2.5: check if the Python packages are installed

pip list

Exercise 2.6: use the virtual environment

Not now :-)

Exercise 2.7: deactivate the virtual environment

deactivate

Exercise 3: export and import a virtual environment

  • Rehearse creating a Python virtual environment

  • Export a virtual environment from a step-by-step instruction

  • Import a virtual environment from a step-by-step instruction

In this exercise, we export and import a virtual environment. Additionally, you get to rehearse to create a virtual environment: as we have done this earlier, the answers will now be hidden :-)

flowchart TD
  load_modules[1.Load modules]
  create[2.Create]
  activate[3.Activate]
  install_libraries[4.Install Python libraries]
  check[5.Check installed Python libraries]
  use[6.Use]
  deactivate[7.Deactivate]

  load_modules --> create
  create --> activate
  activate --> install_libraries
  install_libraries --> check
  check --> use
  use --> deactivate
  deactivate --> activate

Exercise 3.1: load the modules needed

Load the modules for Python 3.11.x.

Exercise 3.2: create the virtual environment

Create a virtual environment with the name analysis.

Exercise 3.3: activate the virtual environment

Activate the virtual environment.

Exercise 3.4: install Python packages

Installing these packages takes around 13 minutes.

Create a file called requirements.txt, with the following content:

numpy==1.22.3
matplotlib==3.5.2
pandas==1.4.2

Install packages by using the requirements.txt file:

pip install -r requirements.txt

Exercise 3.5: check if the Python packages are installed

Check that the packages were installed.

Exercise 3.6: use the virtual environment

Export the Python packages our virtual environment uses:

pip freeze > requirements.txt

View the file requirements.txt.

Exercise 3.7: deactivate the virtual environment

Deactivate the virtual environment