Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating conda environment in bash script that runs on startup

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.

like image 775
Robbie Barrat Avatar asked Feb 01 '19 11:02

Robbie Barrat


1 Answers

You might have solved the issue yet, but for future viewers, this worked for me:

if [ -f "/path/to/anaconda3/etc/profile.d/conda.sh" ]; then
    . "/path/to/anaconda3/etc/profile.d/conda.sh"
    CONDA_CHANGEPS1=false conda activate myenv
fi

Add this instead of conda activate myenv.

like image 123
gwido Avatar answered Sep 22 '22 13:09

gwido