pythonconda

Make conda activate another script for a given environment


I have a conda environment called hep. I needed to build a package from source i.e. root.cern.ch since I am using some features only avaialble in the master branch. In order to be able use import ROOT in the hep environment. I need to source root in my terminal. How can I make conda activate hep do the sourcing of the package automatically?


Solution

  • To automatically source ROOT setup script whenever you activate hep, you can write a custom activation script, as Conda allows you to run shell commands automatically during activation by creating/modifying an activation script specific to the environment:

    1. Find where your Conda environments are stored (it is usually "~/anaconda3/envs/" for Linux/macOS or "C:\Users<username>\anaconda3\envs<env-name>`" for Windows
    2. Inside hep environment’s "etc/conda/activate.d" directory, create a shell script that will run automatically on activation:
      mkdir -p ~/anaconda3/envs/hep/etc/conda/activate.d
      
    3. Create new shell script in "activate.d" directory (e.g., "source_root.sh":
      nano ~/anaconda3/envs/hep/etc/conda/activate.d/source_root.sh
      
      In this file, add the below to source the ROOT installation:
      #!/bin/bash
      source /path/to/your/root/installation/bin/thisroot.sh
      
      (of course, you can replace "/path/to/your/root/installation/bin/thisroot.sh" with the actual path to the ROOT script you need to source

    This will automatically source the ROOT setup script whenever "conda activate hep" runs. By placing it in "activate.d" dir, Conda ensures it runs only for hep