在 shell 脚本中激活 conda 环境

在 shell 脚本中激活 conda 环境

我有一个运行 Ubuntu 18.04 的 EC2 实例。我在其中实现了一个 Python Flask 服务器,我希望它在我启动时立即开始运行。我想用 来做到这一点/etc/rc.local/

当我登录并按顺序运行以下命令时,conda 环境激活,因此我的Flask服务器运行良好。

cd ~
cd pult
conda activate pult
export FLASK_APP=hello.py
flask run

这是我在 中写入的内容/etc/rc.local。该文件不存在,因此我创建了一个。

#!/bin/bash
cd ~
cd pult
sudo ln -s /home/ubuntu/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh
conda activate pult
export FLASK_APP=hello.py
flask run

当我运行时/bin/bash /etc/rc.local出现此错误,并且 conda 环境无法激活。

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If your shell is Bash or a Bourne variant, enable conda for the current user with

    $ echo ". /home/ubuntu/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc

or, for all users, enable conda with

    $ sudo ln -s /home/ubuntu/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT
put conda's base (root) environment on PATH.  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.  You should manually remove the line that looks like

    export PATH="/home/ubuntu/anaconda3/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^

我也尝试过:

  • 放入source ~/.bashrc第 2 行(运行此命令应放入condaPATH 中)
  • 代替export PATH="/home/ubuntu/anaconda3/bin:$PATH"第 4 行

但它们都给出了相同的错误。我该怎么办,以便/bin/bash /etc/rc.local 将激活 conda 环境

答案1

尝试使用

source /home/ubuntu/anaconda3/etc/profile.d/conda.sh

然后做

conda activate pult

干杯!!

答案2

为了供未来想要在远程集群上运行的 shell 脚本中激活 conda 环境的用户参考,请通过在 shell 脚本中添加以下行来执行此操作:

source /home/username/miniconda3/bin/activate myenv

代替conda activate myenv

相关内容