1

I can activate my env in my shell

> conda activate env

When I'm in a bash script, I can check the version of my conda but I can't activate my env

#!/bin/bash

conda --version
> conda 4.7.12

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

    $ conda init <SHELL_NAME>

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

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

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

Here is my .bashrc about conda initialization

  8 # User specific aliases and functions
  9 
 10 # >>> conda initialize >>>
 11 # !! Contents within this block are managed by 'conda init' !!
 12 __conda_setup="$('/work/***/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
 13 if [ $? -eq 0 ]; then
 14     eval "$__conda_setup"
 15 else
 16     if [ -f "/work/***/miniconda3/etc/profile.d/conda.sh" ]; then
 17         . "/work/***/miniconda3/etc/profile.d/conda.sh"
 18     else
 19         export PATH="/work/***/miniconda3/bin:$PATH"
 20     fi
 21 fi
 22 unset __conda_setup
 23 # <<< conda initialize <<<

I found from here : Conda command working in command prompt but not in bash script that I can add in my bash script before activate my env :

source /work/***/miniconda3/etc/profile.d/conda.sh

And it works (my env is activated) but I don't want to add this line at the begginning of all my bash scripts since I already have it in my .bashrc. I also found from here : Python - Activate conda env through shell script that I can source my .bashrc when I run my script with

bash -i shell_script.sh

It is also working (my env is activated).

How can I source automatically my .bashrc each time I run a script without adding a line in each script and without adding -i ? Can I do this with my .bashrc_profile for example ?

Elysire
  • 693
  • 10
  • 23
  • 1
    When you run a bash script, it normally does not source .bashrc, unless you use the `-i` script. That's why `bash -i` works. Have a look at the _bash man page_, where it is described under what circumstances which files are automatically sourced by bash. – user1934428 Jan 09 '20 at 14:09
  • You can add . ~/.bashrc to the first line of your script – bob dylan Jan 09 '20 at 14:16
  • I already know why `-i` is working, I'm just not satisfy with that solution. Adding `~/.bashrc` is just another way to do the same thing as `bash -i shell_script.sh`, I don't want to add a line in each script. I eddited the end of my post so you can understand what I would like as a solution. – Elysire Jan 09 '20 at 14:24
  • ```~/.bash_profile``` is only sourced by bash when started in interactive login mode. You can create a new master script, run your ```env activate script``` in there and then call your required final scripts. – Dark Matter Jan 09 '20 at 15:04
  • Ok I understand why I can't use `.bash_profile` to solve this. A master script can be a solution even if I was hopping something better. Too bad Conda became a bash function and is not an executable anymore. – Elysire Jan 09 '20 at 16:13
  • Have you tried editing the shebang? E.g. `#!/bin/bash -l`, then run with `./shell_script.sh`. – merv Feb 01 '20 at 05:43
  • Yes, I can do `bash -i shell_script.sh` or `bash -l shell_script.sh`, and I can edit the shebang : `#!/bin/bash -i` or `#!/bin/bash -l` then run `./shell_script.sh`. All these solutions are working. It is just not a "permanent" solution since I have to do it for each script. I prefered in last version of conda when we just had to export conda to the path. – Elysire Feb 03 '20 at 08:46

0 Answers0