Setting Up Programming Environment

This page provides instructions for installing the required packages and their dependencies on a local computer or server.

Install miniforge

If you already have a preferred way to manage Python versions and libraries, you can stick to that. Otherwise, we recommend you to install Python3 and all required libraries using Miniforge, a free minimal installer for the package, dependency, and environment manager Conda.

  • Please follow the installation instructions to install Miniforge.

  • After installation, verify that Conda is installed correctly by running:

$ conda --version

# Example output: conda 24.11.2

Configure programming environment

With Conda installed, run the command below to install all required packages and depenencies

$ conda env create --yes -f https://raw.githubusercontent.com/ENCCS/practical-machine-learning/main/content/env/environment.yml

This will create a new environment called practical_machine_learning, which can be activated with

$ conda activate practical_machine_learning

Warning

Remember to activate your programming environment each time before running code examples. This ensures that the correct Python version and all required dependencies are available. If you forget to activate it, you may encounter errors or missing packages.

Validate programming environment

Once the programming environment is fully set up, open a new terminal (just as you should do each time before running code examples), activate the programming environment, and launch JupyterLab by running the command below.

$ conda activate practical_machine_learning

$ jupyter lab

This will start a Jupyter server and automatically open the JupyterLab interface in your web browser.

To verify that all required packages are properly installed, follow these steps:

  • Open JupyterLab (see instructions above).

  • Create a new Jupyter Notebook by selecting FileNewNotebook.

  • Copy the code examples listed below into a cell of the notebook.

  • Run the cell (press Shift + Enter or click the Run button).

import numpy;      print('Numpy version: ',            numpy.__version__)
import pandas;     print('Pandas version: ',           pandas.__version__)
import scipy;      print('Scipy version: ',            scipy.__version__)
import matplotlib; print('Matplotlib version: ',       matplotlib.__version__)
import seaborn;    print('Seaborn version: ',          seaborn.__version__)
import sklearn;    print('Scikit-learn version: ',     sklearn.__version__)
import keras;      print('Keras version: ',            keras.__version__)
import tensorflow; print('Tensorflow version: ',       tensorflow.__version__)
import torch;      print('Pytorch version: ',          torch.__version__)
import umap;       print('Umap-learn version: ',       umap.__version__)
import notebook;   print('Jupyter Notebook version: ', notebook.__version__)

You should see output similar to the figure below. The exact package versions may vary depending on when you installed them.

../_images/0-verification-programming-environment.png

If the code runs without errors, it means the packages are correctly installed and your programming environment is ready for use. If you encounter an error (e.g., ModuleNotFoundError), it indicates that a package may not have been installed properly. In that case, please double-check your programming environment setup or bring the issue to the on-boarding session for assistance.

Warning

If you encounter an error like the one shown in the figure below, it usually means PyTorch is trying to load a DLL (such as fbgemm.dll), but one of its dependencies is missing or incompatible. The most common causes are a missing Microsoft Visual C++ runtime or a mismatch between the installed PyTorch build and your Python/OS environment.

../_images/pytorch_error.png

To verify, you can open another Jupyter notebook and test PyTorch again. You should see output similar to the example below.

../_images/pytorch_another_test.png

Note

If you are using VS Code, you can select the installed practical_machine_learning programming environment as follows:

  • Open your project folder in VS Code.

  • In the upper-right corner of the editor window (when working with a Python file or Jupyter Notebook), click on Select Kernel.

  • From the list of Python Environments, locate and choose the practical_machine_learning environment (which you have installed earlier).

  • Once selected, VS Code will use this environment for running Python code and Jupyter Notebooks, ensuring that all required packages are available.

(Optional) Setting Up PyTorch with GPU Support

If your computer has a GPU, you can install PyTorch with GPU support. Below are step-by-step instructions to update the practical_machine_learning programming environment.

First check your CUDA version. Open a terminal (Linux/macOS) or PowerShell (Windows) and run:

nvcc --version

If nvcc is not in your PATH, you can instead run

nvidia-smi

Here is the output from my Windows machine:

../_images/test-cuda-compiler-driver.png

Second, install the required packages and libraries using conda (for CUDA 12.1):

(base) C:\Users\XXX> conda activate practical_machine_learning

(practical_machine_learning) C:\Users\XXX> conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

Third, verify your installation in a Jupyter Notebook. Run the following command and ensure it returns True for torch.cuda.is_available().

import torch

print(torch.__version__)               # 2.5.1
print(torch.cuda.is_available())       # True
print(torch.cuda.get_device_name(0))   # NVIDIA GeForce GT 1030