Conda: Can't execute `conda activate` from bash script

Created on 20 Nov 2018  Â·  59Comments  Â·  Source: conda/conda

Current Behavior

Trying to use conda activate my_env does not work inside a bash script. The workaround is to use source activate my_env but this shouldn't be necessary.

Steps to Reproduce

Write a bash script with the following code inside, let's call it my-script.sh:

conda activate my_env

When you run ./my-script.sh, you get the following output:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If your shell is Bash or a Bourne variant, enable conda for the current user with

    $ echo ". /Users/matthewspeck/anaconda3/etc/profile.d/conda.sh" >> ~/.bash_profile

or, for all users, enable conda with

    $ sudo ln -s /Users/matthewspeck/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT
put conda's base (root) environment on PATH.  To do so, run

    $ conda activate

in your terminal, or to put the base environment on PATH permanently, run

    $ echo "conda activate" >> ~/.bash_profile

Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
your ~/.bash_profile file.  You should manually remove the line that looks like

    export PATH="/Users/matthewspeck/anaconda3/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bash_profile file! ^^^

However, conda is in fact installed correctly, and runs fine in the command line.

The command which conda returns the following:

conda () {
    if [ "$#" -lt 1 ]
    then
        $_CONDA_EXE
    else
        \local cmd="$1"
        shift
        case "$cmd" in
            (activate) _conda_activate "$@" ;;
            (deactivate) _conda_deactivate "$@" ;;
            (install | update | uninstall | remove) $_CONDA_EXE "$cmd" "$@" && _conda_reactivate ;;
            (*) $_CONDA_EXE "$cmd" "$@" ;;
        esac
    fi
}

Expected Behavior

conda should activate in a bash script the same way it does on the command line.

Environment Information

conda info



     active environment : base
    active env location : /Users/matthewspeck/anaconda3
            shell level : 1
       user config file : /Users/matthewspeck/.condarc
 populated config files : /Users/matthewspeck/.condarc
          conda version : 4.5.11
    conda-build version : 3.16.2
         python version : 3.6.6.final.0
       base environment : /Users/matthewspeck/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/osx-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/osx-64
                          https://repo.anaconda.com/pkgs/pro/noarch
          package cache : /Users/matthewspeck/anaconda3/pkgs
                          /Users/matthewspeck/.conda/pkgs
       envs directories : /Users/matthewspeck/anaconda3/envs
                          /Users/matthewspeck/.conda/envs
               platform : osx-64
             user-agent : conda/4.5.11 requests/2.20.0 CPython/3.6.6 Darwin/17.7.0 OSX/10.13.6
                UID:GID : 501:20
             netrc file : None
           offline mode : False

conda config --show-sources


==> /Users/matthewspeck/.condarc <==
ssl_verify: True
channels:
  - defaults

conda list --show-channel-urls


# packages in environment at /Users/matthewspeck/anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py36_0    defaults
alabaster                 0.7.12                   py36_0    defaults
anaconda-client           1.7.2                    py36_0    defaults
anaconda-navigator        1.9.2                    py36_0    defaults
anaconda-project          0.8.2                    py36_0    defaults
appdirs                   1.4.3            py36h28b3542_0    defaults
appnope                   0.1.0            py36hf537a9a_0    defaults
appscript                 1.0.1            py36h1de35cc_1    defaults
asn1crypto                0.24.0                   py36_0    defaults
astroid                   2.0.4                    py36_0    defaults
astropy                   3.0.5            py36h1de35cc_0    defaults
atomicwrites              1.2.1                    py36_0    defaults
attrs                     18.2.0           py36h28b3542_0    defaults
automat                   0.7.0                    py36_0    defaults
babel                     2.6.0                    py36_0    defaults
backcall                  0.1.0                    py36_0    defaults
backports                 1.0                      py36_1    defaults
backports.os              0.1.1                    py36_0    defaults
backports.shutil_get_terminal_size 1.0.0                    py36_2    defaults
beautifulsoup4            4.6.3                    py36_0    defaults
bitarray                  0.8.3            py36h1de35cc_0    defaults
bkcharts                  0.2              py36h073222e_0    defaults
blas                      1.0                         mkl    defaults
blaze                     0.11.3                   py36_0    defaults
bleach                    3.0.2                    py36_0    defaults
blosc                     1.14.4               hd9629dc_0    defaults
bokeh                     1.0.1                    py36_0    defaults
boto                      2.49.0                   py36_0    defaults
bottleneck                1.2.1            py36h1d22016_1    defaults
bzip2                     1.0.6                h1de35cc_5    defaults
ca-certificates           2018.03.07                    0    defaults
certifi                   2018.10.15               py36_0    defaults
cffi                      1.11.5           py36h6174b99_1    defaults
chardet                   3.0.4                    py36_1    defaults
click                     7.0                      py36_0    defaults
cloudpickle               0.6.1                    py36_0    defaults
clyent                    1.2.2                    py36_1    defaults
colorama                  0.4.0                    py36_0    defaults
conda                     4.5.11                   py36_0    defaults
conda-build               3.16.2                   py36_0    defaults
conda-env                 2.6.0                         1    defaults
constantly                15.1.0           py36h28b3542_0    defaults
contextlib2               0.5.5            py36hd66e5e7_0    defaults
coverage                  4.5.1            py36h1de35cc_0    defaults
cryptography              2.3.1            py36hdbc3d79_0    defaults
curl                      7.61.0               ha441bb4_0    defaults
cycler                    0.10.0           py36hfc81398_0    defaults
cython                    0.29             py36h0a44026_0    defaults
cytoolz                   0.9.0.1          py36h1de35cc_1    defaults
dask                      0.20.0                   py36_0    defaults
dask-core                 0.20.0                   py36_0    defaults
datashape                 0.5.4                    py36_1    defaults
dbus                      1.13.2               h760590f_1    defaults
decorator                 4.3.0                    py36_0    defaults
defusedxml                0.5.0                    py36_1    defaults
distributed               1.24.0                   py36_0    defaults
docutils                  0.14             py36hbfde631_0    defaults
entrypoints               0.2.3                    py36_2    defaults
et_xmlfile                1.0.1            py36h1315bdc_0    defaults
expat                     2.2.6                h0a44026_0    defaults
fastcache                 1.0.2            py36h1de35cc_2    defaults
filelock                  3.0.10                   py36_0    defaults
flask                     1.0.2                    py36_1    defaults
flask-cors                3.0.6                    py36_0    defaults
freetype                  2.9.1                hb4e5f40_0    defaults
get_terminal_size         1.0.0                h7520d66_0    defaults
gettext                   0.19.8.1             h15daf44_3    defaults
gevent                    1.3.7            py36h1de35cc_1    defaults
glib                      2.56.2               hd9629dc_0    defaults
glob2                     0.6                      py36_1    defaults
gmp                       6.1.2                hb37e062_1    defaults
gmpy2                     2.0.8            py36h6ef4df4_2    defaults
greenlet                  0.4.15           py36h1de35cc_0    defaults
h5py                      2.8.0            py36h878fce3_3    defaults
hdf5                      1.10.2               hfa1e0ec_1    defaults
heapdict                  1.0.0                    py36_2    defaults
html5lib                  1.0.1                    py36_0    defaults
hyperlink                 18.0.0                   py36_0    defaults
icu                       58.2                 h4b95b61_1    defaults
idna                      2.7                      py36_0    defaults
imageio                   2.4.1                    py36_0    defaults
imagesize                 1.1.0                    py36_0    defaults
importlib_metadata        0.6                      py36_0    defaults
incremental               17.5.0                   py36_0    defaults
intel-openmp              2019.0                      118    defaults
ipykernel                 5.1.0            py36h39e3cac_0    defaults
ipython                   7.1.1            py36h39e3cac_0    defaults
ipython_genutils          0.2.0            py36h241746c_0    defaults
ipywidgets                7.4.2                    py36_0    defaults
isort                     4.3.4                    py36_0    defaults
itsdangerous              1.1.0                    py36_0    defaults
jbig                      2.1                  h4d881f8_0    defaults
jdcal                     1.4                      py36_0    defaults
jedi                      0.13.1                   py36_0    defaults
jinja2                    2.10                     py36_0    defaults
jpeg                      9b                   he5867d9_2    defaults
jsonschema                2.6.0            py36hb385e00_0    defaults
jupyter                   1.0.0                    py36_7    defaults
jupyter_client            5.2.3                    py36_0    defaults
jupyter_console           6.0.0                    py36_0    defaults
jupyter_core              4.4.0                    py36_0    defaults
jupyterlab                0.35.3                   py36_0    defaults
jupyterlab_launcher       0.13.1                   py36_0    defaults
jupyterlab_server         0.2.0                    py36_0    defaults
keyring                   16.0.0                   py36_0    defaults
kiwisolver                1.0.1            py36h0a44026_0    defaults
lazy-object-proxy         1.3.1            py36h1de35cc_2    defaults
libarchive                3.3.3                hc2e69e3_1    defaults
libcurl                   7.61.0               hf30b1f0_0    defaults
libcxx                    4.0.1                h579ed51_0    defaults
libcxxabi                 4.0.1                hebd6815_0    defaults
libedit                   3.1.20170329         hb402a30_2    defaults
libffi                    3.2.1                h475c297_4    defaults
libgfortran               3.0.1                h93005f0_2    defaults
libiconv                  1.15                 hdd342a3_7    defaults
libpng                    1.6.34               he12f830_0    defaults
libsodium                 1.0.16               h3efe00b_0    defaults
libssh2                   1.8.0                h322a93b_4    defaults
libtiff                   4.0.9                hcb84e12_2    defaults
libxml2                   2.9.8                hab757c2_1    defaults
libxslt                   1.1.32               hb819dd2_0    defaults
llvmlite                  0.25.0           py36h8c7ce04_0    defaults
locket                    0.2.0            py36hca03003_1    defaults
lxml                      4.2.5            py36hef8c89e_0    defaults
lz4-c                     1.8.1.2              h1de35cc_0    defaults
lzo                       2.10                 h362108e_2    defaults
markupsafe                1.0              py36h1de35cc_1    defaults
matplotlib                3.0.0            py36h54f8f79_0    defaults
mccabe                    0.6.1                    py36_1    defaults
mistune                   0.8.4            py36h1de35cc_0    defaults
mkl                       2019.0                      118    defaults
mkl-service               1.1.2            py36h6b9c3cc_5    defaults
mkl_fft                   1.0.6            py36hb8a8100_0    defaults
mkl_random                1.0.1            py36h5d10147_1    defaults
more-itertools            4.3.0                    py36_0    defaults
mpc                       1.1.0                h6ef4df4_1    defaults
mpfr                      4.0.1                h3018a27_3    defaults
mpmath                    1.0.0                    py36_2    defaults
msgpack-python            0.5.6            py36h04f5b5a_1    defaults
multipledispatch          0.6.0                    py36_0    defaults
navigator-updater         0.2.1                    py36_0    defaults
nbconvert                 5.4.0                    py36_1    defaults
nbformat                  4.4.0            py36h827af21_0    defaults
ncurses                   6.1                  h0a44026_0    defaults
networkx                  2.2                      py36_1    defaults
nltk                      3.3.0                    py36_0    defaults
nose                      1.3.7                    py36_2    defaults
notebook                  5.7.0                    py36_0    defaults
numba                     0.40.0           py36h6440ff4_0    defaults
numexpr                   2.6.8            py36h1dc9127_0    defaults
numpy                     1.15.4           py36h6a91979_0    defaults
numpy-base                1.15.4           py36h8a80b8c_0    defaults
numpydoc                  0.8.0                    py36_0    defaults
odo                       0.5.1            py36hc1af34a_0    defaults
olefile                   0.46                     py36_0    defaults
openpyxl                  2.5.9                    py36_0    defaults
openssl                   1.0.2p               h1de35cc_0    defaults
packaging                 18.0                     py36_0    defaults
pandas                    0.23.4           py36h6440ff4_0    defaults
pandoc                    1.19.2.1             ha5e8f32_1    defaults
pandocfilters             1.4.2                    py36_1    defaults
parso                     0.3.1                    py36_0    defaults
partd                     0.3.9                    py36_0    defaults
path.py                   11.5.0                   py36_0    defaults
pathlib2                  2.3.2                    py36_0    defaults
patsy                     0.5.1                    py36_0    defaults
pcre                      8.42                 h378b8a2_0    defaults
pep8                      1.7.1                    py36_0    defaults
pexpect                   4.6.0                    py36_0    defaults
pickleshare               0.7.5                    py36_0    defaults
pillow                    5.3.0            py36hb68e598_0    defaults
pip                       18.1                     py36_0    defaults
pkginfo                   1.4.2                    py36_1    defaults
pluggy                    0.8.0                    py36_0    defaults
ply                       3.11                     py36_0    defaults
prometheus_client         0.4.2                    py36_0    defaults
prompt_toolkit            2.0.7                    py36_0    defaults
psutil                    5.4.8            py36h1de35cc_0    defaults
ptyprocess                0.6.0                    py36_0    defaults
py                        1.7.0                    py36_0    defaults
pyasn1                    0.4.4            py36h28b3542_0    defaults
pyasn1-modules            0.2.2                    py36_0    defaults
pycodestyle               2.4.0                    py36_0    defaults
pycosat                   0.6.3            py36h1de35cc_0    defaults
pycparser                 2.19                     py36_0    defaults
pycrypto                  2.6.1            py36h1de35cc_9    defaults
pycurl                    7.43.0.2         py36hdbc3d79_0    defaults
pyflakes                  2.0.0                    py36_0    defaults
pygments                  2.2.0            py36h240cd3f_0    defaults
pylint                    2.1.1                    py36_0    defaults
pyodbc                    4.0.24           py36h0a44026_0    defaults
pyopenssl                 18.0.0                   py36_0    defaults
pyparsing                 2.3.0                    py36_0    defaults
pyqt                      5.9.2            py36h655552a_2    defaults
pysocks                   1.6.8                    py36_0    defaults
pytables                  3.4.4            py36h13cba08_0    defaults
pytest                    3.10.0                   py36_0    defaults
pytest-arraydiff          0.2              py36h39e3cac_0    defaults
pytest-astropy            0.4.0                    py36_0    defaults
pytest-cov                2.6.0                    py36_0    defaults
pytest-doctestplus        0.1.3                    py36_0    defaults
pytest-openfiles          0.3.0                    py36_0    defaults
pytest-remotedata         0.3.1                    py36_0    defaults
python                    3.6.6                hc167b69_0    defaults
python-dateutil           2.7.5                    py36_0    defaults
python-libarchive-c       2.8                      py36_6    defaults
python.app                2                        py36_9    defaults
pytz                      2018.7                   py36_0    defaults
pywavelets                1.0.1            py36h1d22016_0    defaults
pyyaml                    3.13             py36h1de35cc_0    defaults
pyzmq                     17.1.2           py36h1de35cc_0    defaults
qt                        5.9.6                h45cd832_2    defaults
qtawesome                 0.5.2                    py36_0    defaults
qtconsole                 4.4.2                    py36_0    defaults
qtpy                      1.5.2                    py36_0    defaults
readline                  7.0                  h1de35cc_5    defaults
requests                  2.20.0                   py36_0    defaults
rope                      0.11.0                   py36_0    defaults
ruamel_yaml               0.15.46          py36h1de35cc_0    defaults
scikit-image              0.14.0           py36h0a44026_1    defaults
scikit-learn              0.20.0           py36h4f467ca_1    defaults
scipy                     1.1.0            py36h28f7352_1    defaults
seaborn                   0.9.0                    py36_0    defaults
send2trash                1.5.0                    py36_0    defaults
service_identity          17.0.0           py36h28b3542_0    defaults
setuptools                40.5.0                   py36_0    defaults
simplegeneric             0.8.1                    py36_2    defaults
singledispatch            3.4.0.3          py36hf20db9d_0    defaults
sip                       4.19.8           py36h0a44026_0    defaults
six                       1.11.0                   py36_1    defaults
snappy                    1.1.7                he62c110_3    defaults
snowballstemmer           1.2.1            py36h6c7b616_0    defaults
sortedcollections         1.0.1                    py36_0    defaults
sortedcontainers          2.0.5                    py36_0    defaults
sphinx                    1.8.1                    py36_0    defaults
sphinxcontrib             1.0                      py36_1    defaults
sphinxcontrib-websupport  1.1.0                    py36_1    defaults
spyder                    3.3.1                    py36_1    defaults
spyder-kernels            0.2.6                    py36_0    defaults
sqlalchemy                1.2.13           py36h1de35cc_0    defaults
sqlite                    3.24.0               ha441bb4_0    defaults
statsmodels               0.9.0            py36h1d22016_0    defaults
sympy                     1.3                      py36_0    defaults
tblib                     1.3.2            py36hda67792_0    defaults
terminado                 0.8.1                    py36_1    defaults
testpath                  0.4.2                    py36_0    defaults
tk                        8.6.8                ha441bb4_0    defaults
toolz                     0.9.0                    py36_0    defaults
tornado                   5.1.1            py36h1de35cc_0    defaults
tqdm                      4.28.1           py36h28b3542_0    defaults
traitlets                 4.3.2            py36h65bd3ce_0    defaults
twisted                   17.5.0                   py36_0    defaults
typed-ast                 1.1.0            py36h1de35cc_0    defaults
unicodecsv                0.14.1           py36he531d66_0    defaults
unixodbc                  2.3.7                h1de35cc_0    defaults
urllib3                   1.23                     py36_0    defaults
virtualenv                16.1.0                    <pip>
wcwidth                   0.1.7            py36h8c6ec74_0    defaults
webencodings              0.5.1                    py36_1    defaults
werkzeug                  0.14.1                   py36_0    defaults
wheel                     0.32.2                   py36_0    defaults
widgetsnbextension        3.4.2                    py36_0    defaults
wrapt                     1.10.11          py36h1de35cc_2    defaults
xlrd                      1.1.0                    py36_1    defaults
xlsxwriter                1.1.2                    py36_0    defaults
xlwings                   0.14.0                   py36_0    defaults
xlwt                      1.2.0            py36h5ad1178_0    defaults
xz                        5.2.4                h1de35cc_4    defaults
yaml                      0.1.7                hc338f04_2    defaults
zeromq                    4.2.5                h0a44026_1    defaults
zict                      0.1.3                    py36_0    defaults
zlib                      1.2.11               hf3cbc9b_2    defaults
zope                      1.0                      py36_1    defaults
zope.interface            4.6.0            py36h1de35cc_0    defaults
zstd                      1.3.3                h2a6be3a_0    defaults

s-3 source-community tag-shell type-bug

Most helpful comment

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

* EDIT *
In the commands above, replace ~/anaconda3/ with the path to your miniconda / anaconda installation. You can find that out by running: conda info | grep -i 'base environment'

All 59 comments

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

* EDIT *
In the commands above, replace ~/anaconda3/ with the path to your miniconda / anaconda installation. You can find that out by running: conda info | grep -i 'base environment'

@nehaljwani How would you do this, for example, in a makefile, and is there a simple way to get the path to conda.sh programmatically (say from the name of the environment or an environment.yml file)?

That seems a messy solution to the issue. Now there is a source line in your bash scripts that has to explicitly reference your conda installation path, making them not very portable.

If you use something like source $CONDA_PREFIX/etc/profile.d/conda.sh to get around this, it then makes the script error in everything but the base environment. You could comment in your script that it can only be run from the base, but again: messy.

Is there a conda variable that always returns your base path regardless of your current location?

@rbutleriii

Is there a conda variable that always returns your base path regardless of your current location?

Try this:

CONDA_BASE=$(conda info --base)

@nehaljwani When conda is installed and a user's .bashrc is modified, should the following lines be added to .bashrc? That might save the users some headache. It enables conda to be called from bash scripts without sourcing $(conda info --base)/etc/profile.d/conda.sh.

export -f conda
export -f __conda_activate
export -f __conda_reactivate
export -f __conda_hashr

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

that works perfectly~!!!

I'm trying that, but get:

➜  /Users/michael  source ~/miniconda3/etc/profile.d/conda.sh
/Users/michael/miniconda3/etc/profile.d/conda.sh:30: parse error near `}'

I'm on Mojave and installed with Miniconda3-latest-MacOSX-x86_64.pkg

This is not a non-issue @nehaljwani. As a user, I would expect that when I run "conda init", that it would prepare my shell for use of "conda activate" everywhere, including within scripts. But it doesn't.

The expected behavior should be exactly what @mjspeck specified, namely that "conda should activate in a bash script the same way it does on the command line".

@rbutleriii is correct that your proposal of adding the line ~/$MY_CONDA_INSTALLATION/etc/profile.d/conda.sh before calling "conda activate" now directly references your personal conda installation. That means that tools that generate "conda activate" calls (e.g. Nextflow) would have to be hand-patched for them to work correctly.

@stuarteberg proposed a perfectly reasonable solution that works (I tested it and yes it works), namely that conda init could add the following to your ~/.bashrc:

export -f conda
export -f __conda_activate
export -f __conda_reactivate
export -f __conda_hashr

I respectfully ask that you re-open this issue and use @stuarteberg's solution (or some other solution) fulfill the expected behavior requested by @mjspeck (i.e. "conda should activate in a bash script the same way it does on the command line"), because that behavior is (I believe) what conda's users are expecting. @stuarteberg's solution would be very easy to implement, and would indeed drastically reduce headaches.

Thank you!!

@jasonsydes I fully agree.

I also had to add export -f __add_sys_prefix_to_path to my .bashrc in order to get rid of the __add_sys_prefix_to_path: command not found error.

Also note that, until this get fixed, you need to keep these export statements outside of the # >>> conda initialize >>> block so that it doesn't get removed in case you later run conda init bash

+1 for re-opening

I have noticed that you can do:

eval "$(conda shell.bash hook)"
conda activate <env-name>

Happy to reopen this, but we have no intention of doing development on this any time soon. The shell scripts are devilishly complicated, and we don't have time to get into them right now. What looks like a "simple change" may break a terrible number of seemingly unrelated stuff.

Community PRs are most welcome.

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

this works

This fixed the issue in my makefile: https://stackoverflow.com/a/55696820/4279266

I think a cleaner way is:

eval "$(conda shell.bash hook)"
conda activate my_env

On the windows environment use "anaconda prompt" instead of "command prompt".

Go to search.
Type "anaconda prompt" and open it.
Then type "conda activate

Happy to reopen this, but we have no intention of doing development on this any time soon. The shell scripts are devilishly complicated, and we don't have time to get into them right now. What looks like a "simple change" may break a terrible number of seemingly unrelated stuff.

Community PRs are most welcome.

If you don't trust the project testing enough for doing delicate changes, how can you trust community PRs doing delicate changes?

It almost feels automation-hostile... for now this seems to work:

echo "export -f conda" >> "$HOME/.bashrc" 
echo "export -f __conda_activate" >> "$HOME/.bashrc" 
echo "export -f __conda_reactivate" >> "$HOME/.bashrc" 
echo "export -f __conda_hashr" >> "$HOME/.bashrc" 
echo "export -f __add_sys_prefix_to_path" >> "$HOME/.bashrc" 
echo 'eval "$(conda shell.bash hook)"' >> "$HOME/.bashrc" 

I wonder how long before I run into the next issue....

EDIT: Well, in some cases it does, in others it doesn't. In the latter cases it seems to help to explicitly execute eval "$(conda shell.bash hook)" at the beginning of the script, even though it already ran when .bashrc was sourced...

For those wanting to work around the above suggestions, source activate likely has the expected behavior, and hopefully one day conda activate will work as expected too. (We felt uncomfortable adding any of the above suggestions into a maintained codebase.)

Another solution is to call your script with bash -i to invoke your interactive environment.

@scopatz -- I just learned that little trick as well. Do you have any idea what it does?

@abalter - yeah it basically builds the activation script in $(conda shell.bash hook) and then runs it in the current interpreter session (eval), which allows conda activate <env-name> to work.

@abalter
The -i option starts bash in interactive mode that means that it loads your ~/.bashrc file, and you probably have conda activation there.

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

Works perfect for me :) Thanks a ton.

most cleaner option will be

eval "$(conda shell.bash hook)"
conda activate my_env

@abalter
The -i option starts bash in interactive mode that means that it loads your ~/.bashrc file, and you probably have conda activation there.

This gets to another question and the reason I was looking at this issue. On linux systems, why is ~/.bashrc modified rather than ~/.bash_profile? A non-interactive shell will not normally source ~/.bashrc.

What is the recommended method to activate a conda environment from a non-interactive shell?

This gets to another question and the reason I was looking at this issue. On linux systems, why is ~/.bashrc modified rather than ~/.bash_profile? A non-interactive shell will not normally source ~/.bashrc.

In Ubuntu (and in other distributions that I can't remember), by default ~/.bash_profile sources ~/.profile, which in turns sources ~/.bashrc. So ~/.bashrc will be loaded regardless of it's interactive or not.

I have noticed that you can do:

eval "$(conda shell.bash hook)"
conda activate <env-name>

Helped activating my prod env when building my docker image :)

So this is gotten to be a pretty confusing thread.

@msarahan: I understand that this is a complicated process, and conda does not want to add anything to conda without through testing, but for now:

Is there a recommendation for how to automate manipulation of conda environments? My use case at hand is to want to automate start up scripts -- make an environment, add some packages, active it, run an app in it.

It seems there is not recommended way to do that with a shell script.

Would a python script be easier?

Or do we just give up and not try to use conda environments in that way at this point?

Thanks

The eval and then activate option works on direnv too, that basically spawns bash to collect exported variables or execute commands

Adding eval "$(conda shell.bash hook)" to bashrc automatically resolve this issue on startup for Git Bash within Vscode.

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

thank you so much

it doesn't work on cygwin, it has failed with the error message

$ source /cygdrive/c/tools/miniconda/etc/profile.d/conda.sh
$ conda activate base
$ conda env list
: No such file or directoryiconda/Scripts/conda.exe
$

more verbose log output


Click to expand

```
$ source /cygdrive/c/tools/miniconda/etc/profile.d/conda.sh

  • source /cygdrive/c/tools/miniconda/etc/profile.d/conda.sh
    ++ export CONDA_EXE=C:/tools/miniconda/Scripts/conda.exe
    ++ CONDA_EXE=C:/tools/miniconda/Scripts/conda.exe
    ++ export _CE_M=
    ++ _CE_M=
    ++ export _CE_CONDA=
    ++ _CE_CONDA=
    ++ export CONDA_PYTHON_EXE=C:/tools/miniconda/python.exe
    ++ CONDA_PYTHON_EXE=C:/tools/miniconda/python.exe
    ++ '[' -z '' ']'
    ++ export CONDA_SHLVL=0
    ++ CONDA_SHLVL=0
    ++ '[' -n x ']'
    ++ '[' -n x ']'
    +++ dirname C:/tools/miniconda/Scripts/conda.exe
    ++ PATH=C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
    ++ export PATH
    ++ '[' -z x ']'

$ conda activate base

  • conda activate base
  • '[' 2 -lt 1 ']'
  • local cmd=activate
  • shift
  • case "$cmd" in
  • __conda_activate activate base
  • '[' -n '' ']'
  • local cmd=activate
  • shift
  • local ask_conda
  • CONDA_INTERNAL_OLDPATH=C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • __add_sys_prefix_to_path
  • '[' -n '' ']'
    ++ dirname C:/tools/miniconda/Scripts/conda.exe
  • SYSP=C:/tools/miniconda/Scripts
    ++ dirname C:/tools/miniconda/Scripts
  • SYSP=C:/tools/miniconda
  • '[' -n x ']'
  • PATH=C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • PATH=C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • PATH=C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • PATH=C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • PATH=C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • PATH=C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • export PATH
    ++ PS1='[\e]0;\w\a]\n[\e[32m]\u@\h [\e[33m]\w[\e[0m]\n\$ '
    ++ C:/tools/miniconda/Scripts/conda.exe shell.posix activate base
  • ask_conda='PS1='\''(base) [\e]0;\w\a]\n[\e[32m]\u@\h [\e[33m]\w[\e[0m]\n\$ '\''
    export PATH='\''/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin'\''
    export CONDA_PREFIX='\''C:\tools\miniconda'\''
    export CONDA_SHLVL='\''1'\''
    export CONDA_DEFAULT_ENV='\''base'\''
    export CONDA_PROMPT_MODIFIER='\''(base) '\''
    export CONDA_EXE='\''/cygdrive/c/tools/miniconda/Scripts/conda.exe'\''
    export _CE_M='\'''\''
    export _CE_CONDA='\'''\''
    'xport CONDA_PYTHON_EXE='\''/cygdrive/c/tools/miniconda/python.exe'\''
  • rc=0
  • PATH=C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin
  • eval 'PS1='\''(base) [\e]0;\w\a]\n[\e[32m]\u@\h [\e[33m]\w[\e[0m]\n\$ '\''
    export PATH='\''/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin'\''
    export CONDA_PREFIX='\''C:\tools\miniconda'\''
    export CONDA_SHLVL='\''1'\''
    export CONDA_DEFAULT_ENV='\''base'\''
    export CONDA_PROMPT_MODIFIER='\''(base) '\''
    export CONDA_EXE='\''/cygdrive/c/tools/miniconda/Scripts/conda.exe'\''
    export _CE_M='\'''\''
    export _CE_CONDA='\'''\''
    'xport CONDA_PYTHON_EXE='\''/cygdrive/c/tools/miniconda/python.exe'\''
    '+ PS1='(base) [\e]0;\w\a]\n[\e[32m]\u@\h [\e[33m]\w[\e[0m]\n\$
    ++ export $'PATH=/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
    ++ PATH=$'/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
    '+ export 'CONDA_PREFIX=C:\tools\miniconda
    '+ CONDA_PREFIX='C:\tools\miniconda
    ++ export $'CONDA_SHLVL=1\r'
    ++ CONDA_SHLVL=$'1\r'
    ++ export $'CONDA_DEFAULT_ENV=base\r'
    ++ CONDA_DEFAULT_ENV=$'base\r'
    '+ export 'CONDA_PROMPT_MODIFIER=(base)
    '+ CONDA_PROMPT_MODIFIER='(base)
    ++ export $'CONDA_EXE=/cygdrive/c/tools/miniconda/Scripts/conda.exe\r'
    ++ CONDA_EXE=$'/cygdrive/c/tools/miniconda/Scripts/conda.exe\r'
    ++ export $'_CE_M=\r'
    ++ _CE_M=$'\r'
    ++ export $'_CE_CONDA=\r'
    ++ _CE_CONDA=$'\r'
    ++ export $'CONDA_PYTHON_EXE=/cygdrive/c/tools/miniconda/python.exe\r'
    ++ CONDA_PYTHON_EXE=$'/cygdrive/c/tools/miniconda/python.exe\r'
  • '[' 0 '!=' 0 ']'
  • __conda_hashr
  • '[' -n '' ']'
  • '[' -n '' ']'
  • hash -r
    (base)
    conda env list
  • conda env list
  • '[' 2 -lt 1 ']'
  • local cmd=env
  • shift
  • case "$cmd" in
  • CONDA_INTERNAL_OLDPATH=$'/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • __add_sys_prefix_to_path
  • '[' -n $'\r' ']'
  • '[' -n x ']'
    ++ dirname $'/cygdrive/c/tools/miniconda/Scripts/conda.exe\r'
    bash: dirname: command not found
  • SYSP=
  • '[' -n x ']'
  • PATH=$'/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • PATH=$'/Scripts:/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • PATH=$'/Library/bin:/Scripts:/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • PATH=$'/Library/usr/bin:/Library/bin:/Scripts:/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • PATH=$'/Library/mingw-w64/bin:/Library/usr/bin:/Library/bin:/Scripts:/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • PATH=$':/Library/mingw-w64/bin:/Library/usr/bin:/Library/bin:/Scripts:/bin:/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • export PATH
  • $'/cygdrive/c/tools/miniconda/Scripts/conda.exe\r' $'\r' $'\r' env list
    : No such file or directoryiconda/Scripts/conda.exe
  • local t1=127
  • PATH=$'/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/tools/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:C:/tools/miniconda:C:/tools/miniconda/Library/mingw-w64/bin:C:/tools/miniconda/Library/usr/bin:C:/tools/miniconda/Library/bin:C:/tools/miniconda/Scripts:C:/tools/miniconda/bin:C:/tools/miniconda/Scripts/condabin:/usr/local/bin:/usr/bin\r'
  • return 127
    (base)

```

for some reason conda init bash doesn't respect line-feeds, converting them to CRLF, then in conda.sh we see CRs or '\r', also CONDA_PREFIX returned as Windows-native path

$ /cygdrive/c/tools/miniconda/Scripts/conda.exe shell.posix activate base
+ /cygdrive/c/tools/miniconda/Scripts/conda.exe shell.posix activate base
PS1='(base) \[\033[1;36m\]18:50:35\[\e[0m\] \[\033[1;34m\]\w\[\e[0m\] \[\033[1;31m\]$\[\e[0m\] '
export PATH='/cygdrive/c/tools/miniconda:/cygdrive/c/tools/miniconda/Library/mingw-w64/bin:/cygdrive/c/tools/miniconda/Library/usr/bin:/cygdrive/c/too
ls/miniconda/Library/bin:/cygdrive/c/tools/miniconda/Scripts:/cygdrive/c/tools/miniconda/bin:/cygdrive/c/tools/miniconda/condabin:/usr/local/bin:/usr/
bin'
export CONDA_PREFIX='C:\tools\miniconda'
export CONDA_SHLVL='1'
export CONDA_DEFAULT_ENV='base'
export CONDA_PROMPT_MODIFIER='(base) '
export CONDA_EXE='/cygdrive/c/tools/miniconda/Scripts/conda.exe'
export _CE_M=''
export _CE_CONDA=''
export CONDA_PYTHON_EXE='/cygdrive/c/tools/miniconda/python.exe'

We do not support Cygwin yet. PRs welcome. The only Unix shells we support at present are the MSYS2 shell and Git for Windows.

I have stumpled across this problem a few days ago, too. Apparently, conda is not fully/properly initialised for non-interactive shell sessions/environments.

I am using bash, and have set up a JupyterLab with conda in my home directory doing

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p miniconda3
${HOME}/miniconda3/bin/conda init bash
${HOME}/miniconda3/bin/conda config --set auto_activate_base false
${HOME}/miniconda3/bin/conda install -n base jupyterlab

If I do in a fresh bash login session (on an HPC system, but that is not really important here)

conda activate base && jupyter lab --ip=$(hostname) --no-browser

then everything works as expected. If I set up a bash script that contains

#!/bin/bash
conda activate base && jupyter lab --ip=$(hostname) --no-browser

and that I execute with

./jupyterlab.sh

I get the following error

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

    $ conda init <SHELL_NAME>

[...]

As my .bashrc already contains the necessary conda initialisation part, I tried to investigate the problem a little bit deeper, and have found my script to work if I explicitely set an interactive bash environment by using the interactive flag in the header

#!/bin/bash -i
conda activate base && jupyter lab --ip=$(hostname) --no-browser

In the end, however, I have decided to go with

#!/usr/bin/env bash
eval "$(conda shell.bash hook)" # properly initialise non-interactive shell
conda activate base && \
jupyter lab --ip=$(hostname) --no-browser

which also works as expected.

I don't really know, if this is of any help here. But I wanted to leave a quick note about the aspect of interactive vs. non-interactive shell sessions/environments, that has not yet explicitely been mentioned above.

(For completion, I have Ubuntu 18.04.4 LTS with bash 4.4.20 and conda 4.8.3.)

@kathoef
Somebody decided in the name of efficiency (?) to place the following code in the default .bashrc

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

Conda initialisation code is inserted at the end of .bashrc. So, the sourcing terminates before the conda initialisation code is reached.

The issue had been bugging me for over a year until I decided to review .bashrc line by line from the beginning and discovered the culprit.

For portability, I currently use the following code:

if [[ -n "$CONDA_EXE" ]]; then
   condaSetup="$($CONDA_EXE 'shell.bash' 'hook' 2> /dev/null)"
   eval "$condaSetup"
else
   if [[ -n "$CONDA_PREFIX" ]]; then
      source ${CONDA_PREFIX%/envs/*}/etc/profile.d/conda.sh
   else
      echo "Unable to initialise conda function. Please check if CONDA variables are exported."
   fi
fi

That works fine as long as you init conda in .bashrc and your job scheduling system passes the environmental variables on.

if you'd run a terminal interactively it should work just fine, for example in ubuntu and friends this option usually disabled by default

you may solve cygwin issue on Windows by creating a new global environment variable SHELLOPTS=igncr and setting relevant paths manually

source ~/anaconda3/etc/profile.d/conda.sh
conda activate

@nehaljwani, thanks, much, works perfectly

@lboy27282 you should add "works perfectly _on your machine_". And this is why nehaljwani's answer was downvoted so much.

Try porting your script to a prod environment where conda installation path might be different and you'll see what I mean.

I've created a conda environment as well as added the path in Google Colab

import sys _ = (sys.path .append("/usr/local/lib/python3.6/site-packages"))

But while activating the environment, It throws the following error.

Screenshot 2020-07-15 at 3 10 09 PM

Also tried as suggested earlier, but I'm not sure where the profile.d/conda.sh is located in my colab notebook.

Screenshot 2020-07-15 at 3 10 34 PM

I'm not sure what is it related to. I know its tricky to work with Google Colab but I'm hoping to get some help here. Thanks.

Also,
the environment is successfully listed in the condo env list as below.

Screenshot 2020-07-15 at 3 07 22 PM

Thanks for finding some time for this. I hope it's resolvable in due time.

I know nothing of Google Colab, but:

sys.path .append("/usr/local/lib/python3.6/site-packages")

Is a Bad Idea -- you really don't want to mix system with conda environments. It's kind of the point to NOT use usr/local -- that's why you have conda.

you should add "works perfectly _on your machine_". And this is why nehaljwani's answer was downvoted so much.

@JivanRoquet You're not being fair. Why don't you say @nehaljwani's answer was upvoted so much? Before this issue is officially fixed (which is surprising that we're already in 2020 and it still is not!), it is the easiest solution to try first.

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

and in sh:

I think a cleaner way is:

eval "$(conda shell.bash hook)"
conda activate my_env

this works

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

Any idea how to undo this? Now whenever I open Git bash, it automatically activates the base Conda environment.

open bash profile file and remove lines added by conda, restart terminal
windows

open bash profile file and remove lines added by conda, restart terminal windows

Thank you, this works. Is there a way to achieve the same effect without having Conda activate automatically every time?

As a side note, it seems conda init disabled the password prompt (SSH agent) that I usually get when opening a new git bash terminal. Can't seem to get it back.

you may try to disable base env activation by setting configuration
parameter:

conda config --set auto_activate_base false

or add this line before init script

export CONDA_AUTO_ACTIVATE_BASE="false"

you may try to disable base env activation by setting configuration parameter: conda config --set auto_activate_base false or add this line before init script export CONDA_AUTO_ACTIVATE_BASE="false"

Where can I find the init script?

the lines added by conda to your bash profile file is init script

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

For anyone else trying this, this only worked for me when I typed conda init bash in step 4. However, if you're using an SSH agent, this will prevent the password prompt from appearing.

conda init knows how to update the ~/.bashrc so that conda is properly initialized in the interactive bash environment.
It updates what appears inside

# >>> conda initialize >>>
...
# <<< conda initialize <<<

Would it be unreasonable to ask that conda init take a file name (presumably a bash script) containing the requisite delimiters and update that script?

@mjspeck

for Windows:

  1. Install Git Bash
  2. Open Git Bash
  3. type source ~/anaconda3/etc/profile.d/conda.sh
  4. type conda init
  5. Restart your shell
  6. Profit.

How do i restart my shell?

@hwtrost

How do i restart my shell?

Close and open again your terminal window / tab ;)

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

* EDIT *
In the commands above, replace ~/anaconda3/ with the path to your miniconda / anaconda installation. You can find that out by running: conda info | grep -i 'base environment'

What if I am using an ssh? when I do conda info, it just gives me a directory for where the environment is installed in the cluster, not a sh file.

Functions are not exported by default to be made available in subshells. I'd recommend you do:

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Closing as non-issue. Feel free to re-open if you feel otherwise.

* EDIT *
In the commands above, replace ~/anaconda3/ with the path to your miniconda / anaconda installation. You can find that out by running: conda info | grep -i 'base environment'

Thanks! Very helpful for me.

In my case it is because WSL bash doesn't load .bashrc but it loads .bash_profile, so I add lines

if [[ -f ~/.bashrc ]] ; then
    . ~/.bashrc
fi

to my .bash_profile to load .bashrc

@scopatz

eval "$(conda shell.bash hook)"
conda activate

This actually doesn't work for me

Traceback (most recent call last):                                                                             
      File "/home/buechel/miniconda3/lib/python3.7/site-packages/conda/cli/main.py", line 98, in main              
        return activator_main()                                                                                    
      File "/home/buechel/miniconda3/lib/python3.7/site-packages/conda/activate.py", line 630, in main             
        activator = Activator(shell, activator_args)                                                               
      File "/home/buechel/miniconda3/lib/python3.7/site-packages/conda/activate.py", line 136, in __init__         
        raise NotImplementedError()                                                                                
    NotImplementedError                               

This works for me:

1- Add this to your .bashrc file in the home repository (_C:/Users//_), you can create it if the file doesn't exist:

source <path_to_your_anaconda_installation_folder>/anaconda3/etc/profile.d/conda.sh

2- Restart your shell.

conda activate <env-name> should work now

@nehaljwani When conda is installed and a user's .bashrc is modified, should the following lines be added to .bashrc? That might save the users some headache. It enables conda to be called from bash scripts without sourcing $(conda info --base)/etc/profile.d/conda.sh.

export -f conda
export -f __conda_activate
export -f __conda_reactivate
export -f __conda_hashr

Could you elaborate how could I do this?

Was this page helpful?
0 / 5 - 0 ratings