Activating conda environment in "startup applications" script

7,338

I would comment but I don't have enough reputation so instead I am writing my guess here as an answer.

conda init added the following lines to the very end of my .bashrc

# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/<user>/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/<user>/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/<user>/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        export PATH="/home/<user>/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

I am not sure what exactly it does but one way or another it seems to add conda to the path. (Also it properly configures your shell to use conda activate, apparently)

If your script gets run before conda gets "initialized" by those lines, perhaps your bash does not know what conda is at that time. So complains conda: command not found

You could try running these lines before your script and see if it works.

Share:
7,338

Related videos on Youtube

Robbie Barrat
Author by

Robbie Barrat

High school student in WV. Really like making programs with python, especially those pertaining to artificial intelligence or number theory. https://github.com/robbiebarrat

Updated on September 18, 2022

Comments

  • Robbie Barrat
    Robbie Barrat over 1 year

    So I have a python script that generates an animation - and it requires libraries that I have in a conda environment. I need to run this script as soon as my computer turns on, so I've written a short bash script that I added to "startup applications". This bash script runs on startup, and reads like this:

    #!/bin/bash
    
    conda activate myenv
    cd ~/scripts
    python generate.py
    

    When I run this in terminal myself, it's fine, but whenever I turn on the computer, the python part of the script doesn't execute, and when I check errors i find:

    conda: command not found
    

    and then i also see the python script failed to run because it's missing libraries (from the conda environment not activating)

    I have tried adding lines to the bash script replacing "conda activate" with "source activate", I have tried adding echo ". /home/<user>/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc to the bash script, replacing "conda" with /home/barrat/anaconda3/bin/conda, and even adding whoami to the bash script that runs at startup to make sure that i haven't magically become root by chance... none of this has worked. I would really appreciate any help. it's 3 AM and i'm a bit desperate.

    • jena
      jena about 4 years
      have you tried to add e.g. sleep 30 command at the beginning of your code? I think your script is executed before your ~/.bashrc loads
    • Joshua Baboo
      Joshua Baboo almost 3 years
      check if this is relavant: unix.stackexchange.com/a/572951/282629