在 shell 脚本中激活 conda 环境

Jay*_* Yi 9 command-line bash scripts

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

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

cd ~
cd pult
conda activate pult
export FLASK_APP=hello.py
flask run
Run Code Online (Sandbox Code Playgroud)

所以这就是我在 中写的/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
Run Code Online (Sandbox Code Playgroud)

当我运行时/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! ^^^
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

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

但他们都给出了同样的错误。我应该怎么做才能/bin/bash /etc/rc.local 激活 conda 环境

xxb*_*nxx 11

尝试使用

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

然后做

conda activate pult

干杯!!