简体   繁体   English

Conda 命令在命令提示符下工作但不在 bash 脚本中

[英]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).只要我通过 linux 终端(bash shell)使用它,我的 anaconda(4.5.4)就可以正常工作。 However, running conda commands in a bash script does not work at all.但是,在 bash 脚本中运行 conda 命令根本不起作用。

The script test.sh containes these lines:脚本 test.sh 包含以下几行:

#!/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现在,运行bash test.sh导致错误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按照 anaconda 版本 > 4.4 的建议,我的 .bashrc 不包含

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

but

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

Thank you.谢谢你。

I solved the problem thanks to @darthbith 's comment.感谢 @darthbith 的评论,我解决了这个问题。

Since conda is a bash function and bash functions can not be propagated to independent shells (eg opened by executing a bash script), one has to add the line由于conda是一个 bash 函数并且 bash 函数不能传播到独立的 shell(例如通过执行 bash 脚本打开),因此必须添加行

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

to the bash script before calling conda commands.在调用 conda 命令之前转到 bash 脚本。 Otherwise bash will not know about conda.否则 bash 将不知道 conda。

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.如果@randomwalker 的方法对您不起作用,并且在您的脚本在更基本的 shell(例如 sh)中运行时也不起作用,那么您有两个选择。

  1. Add this to your script: eval $(conda shell.bash hook)将此添加到您的脚本中: eval $(conda shell.bash hook)

  2. Call your script with: bash -i <scriptname> so that it runs in your interactive environment.使用以下命令调用您的脚本: bash -i <scriptname>以便它在您的交互式环境中运行。

Let's say you try to access user name with "miky" @ "server" address.First when you login to your user ;假设您尝试使用“miky” @ “server”地址访问用户名。首先,当您登录到您的用户时; learn conda path with " which conda " then probably you will get a path such as "/home/miky/anaconda3/bin/conda" then put your conda commands as follow (in my example i use conda to install a mysql plugin forexample.): shh miky@server -t "/home/miky/anaconda3/bin/conda install -y -c anaconda mysql-connector-python" thats all.使用“ which conda ”学习 conda 路径,然后您可能会得到一个路径,例如“/home/miky/anaconda3/bin/conda”,然后将您的 conda 命令如下(在我的示例中,我使用 conda 来安装 mysql 插件。 ): shh miky@server -t "/home/miky/anaconda3/bin/conda install -y -c anaconda mysql-connector-python"就是这样。

do sudo ln -s /home/<user>/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh and try again.sudo ln -s /home/<user>/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh然后再试一次。 This should activate conda for all users permenantly这应该为所有用户永久激活 conda

source 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM