Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Activate conda env through shell script

I am hoping to run a simple shell script to ease the management around some conda environments. Activating conda environments via conda activate in a linux os works fine in the shell but is problematic within a shell script. Could someone point me into the right direction as to why this is happening?

Example to repeat the issue:

# default conda env $ conda info | egrep "conda version|active environment"      active environment : base           conda version : 4.6.9  # activate new env to prove that it works $ conda activate scratch $ conda info | egrep "conda version|active environment"      active environment : scratch           conda version : 4.6.9  # revert back to my original conda env $ conda activate base   $ cat shell_script.sh #!/bin/bash conda activate scratch  # run shell script - this will produce an error even though it succeeded above $ ./shell_script.sh  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'. 
like image 244
user9074332 Avatar asked Apr 04 '19 03:04

user9074332


People also ask

How can I activate conda environment automatically?

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

How do I run a conda in bash?

In order to make the conda command available in Git Bash, you need to add conda's shell script to your . bashrc file. This is the same file that you store your bash aliases in (such as the sqlite3 alias you probably created when you followed these instructions). Open this folder, then navigate to etc -> profile.

How do you put a conda in the shell?

putting source ~/. bashrc in line 2 (running this should put conda in PATH) putting export PATH="/home/ubuntu/anaconda3/bin:$PATH" in place of line 4.

How to run two Python files in one Conda env?

if you want to use the shell script to run the other python file in the other conda env, just run the other file via the following command. What is the problem with simply doing something like this in your shell: (The conda init is still marked as Experimental, and thus not sure if it is a good idea to use it yet).

Why can't I activate Conda in a script?

The problem is that your script file is run in a sub-shell, and conda is not initialized in this sub-shell. Using conda activate or source activate in shell scripts does not always work and can throw errors like this. An easy work around it to place source ~/miniconda3/etc/profile.d/conda.sh above any conda activate command in the script:

How do I activate Conda in the terminal?

To do so, run $ conda activate in your terminal, or to put the base environment on PATH permanently, run $ echo "conda activate" >> ~/.bashrc Previous to conda 4.4, the recommended way to activate conda was to modify PATH in your ~/.bashrc file.

Can I add a Conda command to my bashrc?

In fact, if you open up your .bashrc, you should be able to see a few lines added by conda teaching your shell where to look for conda commands. The problem with your script, though, lies in the fact that the .bashrc is not sourced by the subshell that runs shell scripts (see this answer for more info).


1 Answers

The error message is rather helpful - it's telling you that conda is not properly set up from within the subshell that your script is running in. To be able to use conda within a script, you will need to (as the error message says) run conda init bash (or whatever your shell is) first. The behaviour of conda and how it's set up depends on your conda version, but the reason for the version 4.4+ behaviour is that conda is dependent on certain environment variables that are normally set up by the conda shell itself. Most importantly, this changelog entry explains why your conda activate and deactivate commands no longer behave as you expect in versions 4.4 and above.

For more discussion of this, see the official conda issue on GitHub.


Edit: Some more research tells me that the conda init function mentioned in the error message is actually a new v4.6.0 feature that allows a quick environment setup so that you can use conda activate instead of the old source activate. However, the reason why this works is that it adds/changes several environment variables of your current shell and also makes changes to your RC file (e.g.: .bashrc), and RC file changes are never picked up in the current shell - only in newly created shells. (Unless of course you source .bashrc again). In fact, conda init --help says as much:

IMPORTANT: After running conda init, most shells will need to be closed and restarted for changes to take effect

However, you've clearly already run conda init, because you are able to use conda activate interactively. In fact, if you open up your .bashrc, you should be able to see a few lines added by conda teaching your shell where to look for conda commands. The problem with your script, though, lies in the fact that the .bashrc is not sourced by the subshell that runs shell scripts (see this answer for more info). This means that even though your non-login interactive shell sees the conda commands, your non-interactive script subshells won't - no matter how many times you call conda init.

This leads to a conjecture (I don't have conda on Linux myself, so I can't test it) that by running your script like so:

bash -i shell_script.sh 

you should see conda activate work correctly. Why? -i is a bash flag that tells the shell you're starting to run in interactive mode, which means it will automatically source your .bashrc. This should be enough to enable you to use conda within your script as if you were using it normally.

like image 188
Niayesh Isky Avatar answered Sep 19 '22 22:09

Niayesh Isky