How to use Python libraries in a micromamba virtual environment
Use of micromamba versus Anaconda
Previously, this section was about the use of Anaconda/conda.
Due to recent licensing problems, the use of anaconda/conda is now discouraged. On the other hand, micromamba is fully compatible with the conda command line, doesn't suffer of this licensing problems, plus, since its resolver is written in C++, it is several order of magnitude faster when managing software installation with respect to anaconda.
A virtual environment is a working, isolated copy of Python that maintains its own files and directories so that a user can work with specific versions of libraries without affecting other Python projects. Virtual environments simplify the clean separation of different projects and avoid problems with different dependencies and version requirements between components.
The micromamba command is the interface for managing virtual installations and environments with the micromamba Python distribution.
In this way, a user can use the Python modules which are not installed on the user interface.
On a user interface
micromamba is available on the user interfaces at CNAF via environment modules:
$ module load mamba
Both the mamba and micromamba aliases are available.
The versions of Python available for mamba can be checked by launching the command:
$ mamba search "^python$"
After this, the user has to choose the name of the virtual environment, for example <yourmambaenv>, and the latest version of Python 3 available (Python 3.13):
$ mamba create -n <yourmambaenv> python=3.13
Thus, several new packages will be installed in a mamba environment in your home directory.
If you want to create a mamba environment living in a different path, e.g. under /opt/exp_software/<experiment>/<yourenvironment>/, you need to specify the path using the -p option:
$ mamba create -p /opt/exp_software/<experiment>/<yourenvironment> python=3.13
At the end of the installation, the virtual environment can be activated by typing:
$ mamba activate <yourmambaenv>
Now, the command prompt has changed to indicate the mamba environment we are currently in.
To install an additional package, denoted for example as "package", only in <yourmambaenv> virtual environment, it is enough to type the command:
$ mamba install -n <yourmambaenv> [package]
Finally, to end the session of the current environment:
$ mamba deactivate
NB: It is not necessary to specify the name of the virtual environment: the one that is currently active will be deactivated.
If a user wants to delete a mamba environment that is no longer necessary, the name of the mamba environment must be indicated:
$ mamba remove -n <yourmambaenv> -all
In an HTCondor job
Let's see how to configure the mamba virtual environment in case it is necessary to use python modules in a HTCondor job using the environment module.
First of all, it is necessary to set the MODULEPATH variable and then load the module.
export MODULEPATH=/opt/exp_software/opssw/modules/modulefiles module load mamba
Then the environment can be enabled as usual:
mamba activate <yourmambaenv>
Other tips
- In case a huge amount of files has to be recalled from tape, it is strongly suggested to do it in agreement with Experiment Support group, which can help and facilitate the procedure.
If you need to build a user interface from scratch you can just run the following commands on your AlmaLinux 9 machine. This guide requires
sudoprivileges.sudo curl -L https://linuxsoft.cern.ch/wlcg/wlcg-el9.repo -o /etc/yum.repos.d/wlcg-el9.repo sudo curl -L https://linuxsoft.cern.ch/wlcg/RPM-GPG-KEY-wlcg -o /etc/pki/rpm-gpg/RPM-GPG-KEY-wlcg sudo curl -L https://repository.egi.eu/sw/production/cas/1/current/repo-files/egi-trustanchors.repo -o /etc/yum.repos.d/egi-trustanchors.repo sudo dnf install -y epel-release sudo dnf config-manager --set-enabled crb sudo dnf install -y ui sudo systemctl enable --now fetch-crl.timer