4

I have a desktop shortcut that has the following Exec line:

Exec=conda activate my_env && my_command

However, this shortcut doesn't launch. To try to debug this, I ran the same command in a bash prompt without .bashrc (since .bashrc doesn't get sourced for desktop shortcuts),

user@pc:~$ bash --norc
bash-4.4$ conda activate my_env

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

$ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

The problem is that conda doesn't detect that it has been initialised already. conda init creates an entry in .bashrc:

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

I tried to copy this entry to my .profile and relogged in, but it still didn't work. Trying to instead conda init inside .profile just duplicates the above entry in .bashrc.

Is it possible to activate a conda environment from a linux desktop shortcut?

Mate de Vita
  • 223
  • 2
  • 8
  • I may be mistaken, but I believe that conda environments are only active in the terminal that they are opened in. In other words, if a conda environment is activated in a terminal window and then you open a second terminal window, the environment will only be active in the first terminal window, but not the second. If this is the case, then using a desktop shortcut will not work. – Eagle_Mike Jan 29 '20 at 16:19
  • You are correct about how conda environments work. However, the environment will, for example, be active in matlab if it's run from an interactive shell (i.e. opening the Terminal and running `conda activate matlab_env && matlab`). This is why I was hoping there'd be a way to do it from a `.desktop` file too, since in my understanding `.desktop` files simply run the `Exec` commands in the login shell. – Mate de Vita Jan 30 '20 at 12:51

2 Answers2

2

Assuming that the conda initialization lines are already in one's .bashrc file, one can run a command in an activated environment by starting a new bash session:

Exec=bash -c "source ~/.bashrc && conda activate matlab_env && matlab"
lebedov
  • 121
  • 4
0

I found it useful to just change the shebang on the top of the file from #!/usr/bin/env python3 to #!/home/jacob/anaconda3/envs/</bin/python3

Jacob Valdez
  • 101
  • 2