Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't execute `conda activate` from bash script #7980

Open
mjspeck opened this issue Nov 20, 2018 · 29 comments
Open

Can't execute `conda activate` from bash script #7980

mjspeck opened this issue Nov 20, 2018 · 29 comments

Comments

@mjspeck
Copy link

@mjspeck mjspeck commented Nov 20, 2018

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

@nehaljwani

This comment has been minimized.

Copy link
Collaborator

@nehaljwani nehaljwani commented Nov 24, 2018

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.

@nehaljwani nehaljwani closed this Nov 24, 2018
@mforbes

This comment has been minimized.

Copy link

@mforbes mforbes commented Feb 23, 2019

@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)?

@rbutleriii

This comment has been minimized.

Copy link

@rbutleriii rbutleriii commented Mar 13, 2019

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?

@stuarteberg

This comment has been minimized.

Copy link
Contributor

@stuarteberg stuarteberg commented Mar 13, 2019

@rbutleriii

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

Try this:

CONDA_BASE=$(conda info --base)
@stuarteberg

This comment has been minimized.

Copy link
Contributor

@stuarteberg stuarteberg commented Mar 13, 2019

@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
@shenweihai1

This comment has been minimized.

Copy link

@shenweihai1 shenweihai1 commented Apr 17, 2019

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~!!!

@kagemusha

This comment has been minimized.

Copy link

@kagemusha kagemusha commented May 7, 2019

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

@jasonsydes

This comment has been minimized.

Copy link

@jasonsydes jasonsydes commented May 14, 2019

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!!

@pheidippides

This comment has been minimized.

Copy link

@pheidippides pheidippides commented May 15, 2019

@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

@stuarteberg

This comment has been minimized.

Copy link
Contributor

@stuarteberg stuarteberg commented May 15, 2019

+1 for re-opening

@scopatz

This comment has been minimized.

Copy link
Contributor

@scopatz scopatz commented May 15, 2019

I have noticed that you can do:

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

This comment has been minimized.

Copy link
Member

@msarahan msarahan commented May 15, 2019

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.

@FlamesoFF

This comment has been minimized.

Copy link

@FlamesoFF FlamesoFF commented Jul 29, 2019

@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.
@Nilanshrajput

This comment has been minimized.

Copy link

@Nilanshrajput Nilanshrajput commented Aug 2, 2019

@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

agitter added a commit to agitter/manubot-rootstock that referenced this issue Aug 3, 2019
Intentionally keep conda activate outside script per
conda/conda#7980
@andrew-schweitzer-analog

This comment has been minimized.

Copy link

@andrew-schweitzer-analog andrew-schweitzer-analog commented Aug 7, 2019

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

@bryant1410

This comment has been minimized.

Copy link
Contributor

@bryant1410 bryant1410 commented Aug 23, 2019

I think a cleaner way is:

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

This comment has been minimized.

Copy link

@Seshirantha Seshirantha commented Aug 23, 2019

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

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

@omry

This comment has been minimized.

Copy link

@omry omry commented Aug 25, 2019

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?

@Toxyl

This comment has been minimized.

Copy link

@Toxyl Toxyl commented Sep 18, 2019

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...

@lmeyerov

This comment has been minimized.

Copy link

@lmeyerov lmeyerov commented Sep 30, 2019

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.)

@abalter

This comment has been minimized.

Copy link

@abalter abalter commented Nov 6, 2019

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?

@scopatz

This comment has been minimized.

Copy link
Contributor

@scopatz scopatz commented Nov 7, 2019

@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.

@technic

This comment has been minimized.

Copy link

@technic technic commented Nov 8, 2019

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

@PallawiSinghal

This comment has been minimized.

Copy link

@PallawiSinghal PallawiSinghal commented Nov 20, 2019

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.

langmm added a commit to cropsinsilico/yggdrasil that referenced this issue Nov 22, 2019
…' because of bug where 'conda activate' cannot be called from within a script conda/conda#7980
@cervantes-loves-ai

This comment has been minimized.

Copy link

@cervantes-loves-ai cervantes-loves-ai commented Nov 26, 2019

most cleaner option will be

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

@DoaneAS

This comment has been minimized.

Copy link

@DoaneAS DoaneAS commented Nov 27, 2019

@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?

@bryant1410

This comment has been minimized.

Copy link
Contributor

@bryant1410 bryant1410 commented Nov 27, 2019

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.

@hzitoun

This comment has been minimized.

Copy link

@hzitoun hzitoun commented Nov 29, 2019

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 :)

@jaimergp jaimergp mentioned this issue Dec 5, 2019
5 of 5 tasks complete
@ChrisBarker-NOAA

This comment has been minimized.

Copy link

@ChrisBarker-NOAA ChrisBarker-NOAA commented Dec 6, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.