Userguide python
Virtual environments¶
A virtual environment in Python is a local, isolated environment in which you can install or uninstall Python packages without interfering with the global environment (or other virtual environments). It usually lives in a directory (location varies depending on whether you use venv, conda or poetry). In order to use a virtual environment, you have to activate it. Activating an environment essentially sets environment variables in your shell so that:
pythonpoints to the right Python version for that environment (different virtual environments can use different versions of Python!)pythonlooks for packages in the virtual environmentpip installinstalls packages into the virtual environment- Any shell commands installed via
pip installare made available
To run experiments within a virtual environment, you can simply activate it
in the script given to sbatch.
UV¶
In many cases, where your dependencies are Python packages, we highly recommend using UV
<https://docs.astral.sh/uv>__, a modern package manager for Python.
In addition to all the same features as pip, it also manages Python installations, virtual environments, and makes your environments easier to reproduce and reuse across compute clusters.
Note
UV is not currently available as a module on the Mila or DRAC clusters at the time of writing. To use it, you first need to install it using this command on a cluster login node:
| Pip/virtualenv command | UV pip equivalent | UV project command (recommended) |
|
|---|---|---|---|
| Create your virtualenv | module load python/3.10then python -m venv |
uv venv_ |
uv init and uv sync |
| Activate the virtualenv | . .venv/bin/activate |
(same) | (same, but often unnecessary) |
| Install a package | activate venv then pip install |
uv pip install_ |
uv add_ |
Run a command (ex. python main.py) |
module load python, then. <venv>/bin/activate, then python main.py |
. <venv>/bin/activate,then python main.py |
uv run python main.py |
| Where are dependencies declared? | Maybe in a requirements.txt, setup.py or pyproject.toml |
Maybe in a requirements.txt, setup.py or pyproject.toml |
always in pyproject.toml |
| Easy to change Python versions? | No | somewhat | Yes: uv python pin <version> oruv sync --python <version> |
While you can use UV as a drop-in replacement for pip, we recommend adopting a project-based workflow:
-
Use
uv initto create a new project. Apyproject.tomlfile will be created. This is where your dependencies are listed. -
Use
uv addto add (anduv removeto remove) dependencies to your project. This will update thepyproject.tomlfile and update the virtual environment. -
Use
uv runto run commands, for exampleuv run python train.py. This will automatically do the following:- Create or update the virtualenv (with the correct Python version) if necessary, based the dependencies in
pyproject.toml. - Activates the virtualenv.
- Runs the command you provided, e.g.
python train.py.
- Create or update the virtualenv (with the correct Python version) if necessary, based the dependencies in
Pip/Virtualenv¶
Pip is the most widely used package manager for Python and each cluster provides
several Python versions through the associated module which comes with pip. In
order to install new packages, you will first have to create a personal space
for them to be stored. The usual solution (as it is the recommended solution
on Digital Research Alliance of Canada clusters) is to use virtual
environments <https://virtualenv.pypa.io/en/stable/>_, although using_uv is now
the recommended way to manage Python installations, virtual environments and dependencies.
Note
We recommend you use UV <https://docs.astral.sh/uv>_ to manage your Python
virtual environments instead of doing it manually.
The previous section gives an overview of how to install it and use it.
First, load the Python module you want to use:
Then, create a virtual environment in your home directory:
Where <env> is the name of your environment. Finally, activate the environment:
You can now install any Python package you wish using the pip command, e.g.
pytorch :
Or Tensorflow :
Conda¶
Another solution for Python is to use miniconda
or anaconda which are also available through
the module command: (the use of Conda is not recommended for Digital Research
Alliance of Canada clusters due to the availability of custom-built packages for pip)
To create an environment (see here for details) using a specific Python version, you may write:
Where<env> is the name of your environment. You can now activate it by doing:
You are now ready to install any Python package you want in this environment.
For instance, to install PyTorch, you can find the Conda command of any version
you want on pytorch's website, e.g:
If you make a lot of environments and install/uninstall a lot of packages, it can be good to periodically clean up Conda's cache:
Mamba¶
When installing new packages with conda install, conda uses a built-in
dependency solver for solving the dependency graph of all packages (and their
versions) requested such that package dependency conflicts are avoided.
In some cases, especially when there are many packages already installed in a conda environment, conda's built-in dependency solver can struggle to solve the dependency graph, taking several to tens of minutes, and sometimes never solving. In these cases, it is recommended to try libmamba.
To install and set the libmamba solver, run the following commands:
# Install miniconda
# (you can not use the preinstalled anaconda/miniconda as installing libmamba
# requires ownership over the anaconda/miniconda install directory)
$ wget https://repo.anaconda.com/miniconda/Miniconda3-py310_22.11.1-1-Linux-x86_64.sh
$ bash Miniconda3-py310_22.11.1-1-Linux-x86_64.sh
# Install libmamba
$ conda install -n base conda-libmamba-solver
By default, conda uses the built-in solver when installing packages, even after
installing other solvers. To try libmamba once, add --solver=libmamba in
your conda install command. For example:
You can set libmamba as the default solver by adding solver: libmamba
to your .condarc configuration file located under your $HOME directory.
You can create it if it doesn't exist. You can also run: