Skip to main content

Guide to Resolving the conda init Error in Slurm

When submitting jobs with SLURM, you may sometimes encounter the CondaError: Run 'conda init' before 'conda activate' error. This error indicates that the Conda environment was not properly initialized before using the conda activate command. Here are the detailed steps to resolve this issue:

1. Initialize the Conda Environment

First, make sure to run the conda init command in the terminal to initialize the Conda environment. This step only needs to be performed once:

conda init

This will add the necessary initialization code to your shell configuration file (such as .bashrc or .zshrc).

2. Modify the sbatch Job Script

Find the Conda explicit initialization script:

miniconda3/etc/profile.d/conda.sh

In the sbatch job script, explicitly load the Conda initialization script, then activate the desired Conda environment. Here is an example script:

#!/bin/bash
#SBATCH --job-name=your_job_name
#SBATCH --output=your_output_file.out
#SBATCH --error=your_error_file.err
#SBATCH --time=01:00:00
#SBATCH --partition=your_partition

# Explicitly load the Conda initialization script
source /home/team28/miniconda3/etc/profile.d/conda.sh

# Activate the Conda environment
conda activate Powerinfer

# Verify Conda version
conda --version

# Run your command
python your_script.py

3. Verify the Path

Make sure the path /home/team28/miniconda3/etc/profile.d/conda.sh is correct and exists. If the path is incorrect, please adjust it according to your actual situation.

4. Submit the Job

Save and submit your sbatch job script:

sbatch your_sbatch_script.sh

5. Check the Output

Check the output and error files to ensure the Conda environment is correctly activated and your script is running properly.

Additional Notes

  • If you are working on a different shell (e.g., zsh), make sure to modify your shell configuration file accordingly.
  • If you are working on a multi-user system, make sure the Conda installation path and environment name are unique and correct.

By following the steps above, you should be able to resolve the CondaError: Run 'conda init' before 'conda activate' error and successfully use Conda environments in SLURM jobs.