5

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.

2 Answers 2

10

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.

2
  • This worked for me. Actually very hard to find this solution !
    – Denis
    Commented May 28, 2019 at 8:11
  • Without CONDA_CHANGEPS1=false if you already have anaconda env initiated that starts your terminal with (base) Commented Jul 10, 2020 at 18:13
0

Well as you are trying to activate an environment to start your scripts, it may also be possible for you to make a startup script itself to do the desired task by using subprocess module from python.

Try making a demo.py script like :

import os
import system
import subprocess
import x

subprocess.run(["command name", "value"]) #for all scripts you want to execute

and then you can put this python script to run at startup. You can start quite a few amount of operations without noticable speed changes to your system and always can monitor it easily by starting the processes one after the other using time.sleep() in between two calls.

2
  • sorry - i think you have misuderstood. I have one script i want to run at startup - and my problem is that i can't activate the conda environment that contains the libraries that this script needs to run. Commented Feb 1, 2019 at 11:44
  • Have you tried adding conda environment and it's python to PATH of your operating system ? If it cannot find the installation of conda it will not be able to find anything from python to it's libraries. Commented Feb 1, 2019 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.