5

After a user suggestion, I'm revising the question to more narrowly focus on the question "why can't I run conda commands from a shell script".

My default conda environment is named "py37". Shell starts, I am able to deactivate:

(py37) pauljohn@delllap-16:Desktop$ conda deactivate
pauljohn@delllap-16:Desktop$

I want to create a shell script with a short name, to do same work. I've tried several variants, e.g., a file called "noconda.sh":

$ cat noconda.sh
#!/bin/bash

eval "conda deactivate"   

That's a fail, clearly:

 pauljohn@delllap-16:bin$ noconda.sh

CommandNotFoundError: Your shell has not been properly configured to use 'conda deactivate'.
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'.

I find that quite a few of the other projects I use don't work correctly if conda features are active.

You might ask what is in my .bashrc:

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

# help with conda bug accessing gsettings:
# https://github.com/conda-forge/glib-feedstock/issues/19
export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/

conda deactivate
conda activate py37
pauljohn32
  • 2,079
  • 21
  • 28
  • Have you tried `source deactivate` – shn Jun 10 '19 at 22:51
  • You could first `conda deactivate` then `conda activate py37` in your `.bashrc`. Then it would be just one `conda deactivate` to get away from conda, rather than 2. – darthbith Jun 11 '19 at 03:28
  • @darthbith. That helps a bit. 1 deactivates is better than 2. But I'd still like to make a shell script to get this done more easily. – pauljohn32 Jun 13 '19 at 14:45
  • @shn "source ..." is replaced by "conda ..." in newer Anaconda. Using source has same effect so far as I can tell (https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html). – pauljohn32 Jun 13 '19 at 14:48

2 Answers2

2

Here is a fix.

In end of ~/.bashrc, insert this:

alias noconda='conda deactivate'

After that, command from shell "noconda" gets the thing done.

Wish I could find shell script to get same done, would be more portable across accounts.

Chiel
  • 1,324
  • 1
  • 11
  • 30
pauljohn32
  • 2,079
  • 21
  • 28
2

According to this answer, if you start the sh file using bash -i noconda.sh, conda deactivate works fine. Check the original answer for the reason.

Tengerye
  • 1,796
  • 1
  • 23
  • 46