Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conda command working in command prompt but not in bash script

my anaconda (4.5.4) works fine as long as I just use it via a linux terminal (bash shell). However, running conda commands in a bash script does not work at all.

The script test.sh containes these lines:

#!/bin/bash
conda --version
conda activate env

Now, running bash test.sh results in the error test.sh: line 2: conda: command not found test.sh: line 3: conda: command not found

As recommended for anaconda version > 4.4 my .bashrc does not contain

export PATH="/opt/anaconda/bin:$PATH",

but

. /opt/anaconda/etc/profile.d/conda.sh

Thank you.

like image 202
randomwalker Avatar asked Oct 12 '18 11:10

randomwalker


People also ask

Can I run Conda commands in a bash script?

Bookmark this question. Show activity on this post. my anaconda (4.5.4) works fine as long as I just use it via a linux terminal (bash shell). However, running conda commands in a bash script does not work at all. The script test.sh containes these lines:

Does Conda activate command not work on Windows 10 command prompt?

We can use conda activatecommand to activate an environment. However, you may find this command does not work on windows 10 command prompt (cmd.exe). In this tutorial, we will introduce you how to fix this problem. For example, if you have typed command below: conda activate py3.7 You may get an error like this:

What to do if ‘Conda’ is not working?

Fix: ‘conda’ is not recognized as an internal or external command, operable program or batch file. If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption.

How do I find and adjust the conda path?

Follow the guide down below to discover and adjust the Conda PATH manually in Environment Variables: Access the start menu (bottom-left corner) and search for “anaconda prompt“. Inside the Anaconda Prompt program, run the following command and press Enter to check the location of Conda: where conda Discovering the location of conda


2 Answers

I solved the problem thanks to @darthbith 's comment.

Since conda is a bash function and bash functions can not be propagated to independent shells (e.g. opened by executing a bash script), one has to add the line

source /opt/anaconda/etc/profile.d/conda.sh

to the bash script before calling conda commands. Otherwise bash will not know about conda.

like image 58
randomwalker Avatar answered Oct 06 '22 23:10

randomwalker


If @randomwalker's method doesn't work for you, which it won't any time your script is run in a more basic shell such as sh, then you have two options.

  1. Add this to your script: eval $(conda shell.bash hook)

  2. Call your script with: bash -i <scriptname> so that it runs in your interactive environment.

like image 43
abalter Avatar answered Oct 07 '22 00:10

abalter