Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...