Search code examples
anacondawindows-subsystem-for-linux

Conda not on PATH in non-interactive WSL


I have set up an Ubuntu instance in Windows using WSL, and I am trying to run a script in there. When I run it as

wsl
$PATH
conda -V

I get

-bash: /home/paul/anaconda3/bin:/home/paul/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program: No such file or directory
conda 4.9.2

I have a simple sh script which is

$PATH
conda -V

When I run

wsl --shell-type login --user paul --cd "/home/paul" -e bash -c './whisper.sh'

I get

./whisper.sh: line 2: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program: No such file or directory
./whisper.sh: line 3: conda: command not found

Why is conda not available on the PATH, when I am using shell-type login and the same user?


Solution

  • If you are using bash on wsl-ubuntu, you can find the following lines at the beginning of your ~/.bashrc

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    after this block, your conda() function which anaconda-init created, will be ignore if you are in non-interactive shell.

    Solution:

    You can source anaconda/bin/activate <env name> or source anaconda/etc/profile.d/conda.sh; conda activate <env name> to temporarily init conda environment at the beginning of your script.