在 vagrant shell 配置期间激活 anaconda 环境

Activate anaconda environment during vagrant shell provisioning

我正在使用 vagrant(MacOSX Sierra 上的 1.9.1)将 VirtualBox 上的 ubuntu/xenial64 框提供给 运行 一个 python 应用程序。我无法在配置时使用正常的 shell 命令 source 激活 conda 环境。在我的 bootstrap.sh 中,我有以下几行用于创建新环境然后切换到它。

#!/usr/bin/env bash
set -e # Exit script immediately on first error.
set -x # Print commands and their arguments as they are executed.

/home/ubuntu/miniconda3/bin/conda create --name envmycondaenvironment python=3.5 # environment with python3.5
source activate envgatherurls

我从 vagrant 收到以下错误。

==> default: + source activate envmycondaenvironment
==> default: /tmp/vagrant-shell: line 21: activate: No such file or directory

为什么 shell 脚本找不到 activate?我验证了可以找到 activate/home/ubuntu/miniconda3/bin/ 已经添加到 .bashrc 文件的 PATH 中。

命令activateconda提供,不会自动添加到PATH环境变量中。请注意 bootstrap.sh 脚本以 root 而不是 vagrant 用户身份运行。因此,您需要确保 root 用户的 .bashrc 路径中包含 /home/ubuntu/miniconda3/bin。如果我是你,我宁愿这样做:

#!/usr/bin/env bash
set -e # Exit script immediately on first error.
set -x # Print commands and their arguments as they are executed.

export PATH=/home/ubuntu/miniconda3/bin:$PATH
conda create --name envmycondaenvironment python=3.5 # environment with python3.5
source activate envgatherurls