简体   繁体   English

使用cloud-init激活conda环境

[英]Activating conda environment with cloud-init

We are trying to run batch scripts on load on a AWS EC2 instance using userdata (which I understand is based off of cloud-init). 我们正在尝试使用用户数据(据我了解是基于cloud-init的)在AWS EC2实例上加载时运行批处理脚本。 Since the code runs in a conda environment, we are trying to activate it prior to running the Python/Pandas code. 由于代码在conda环境中运行,因此我们试图在运行Python / Pandas代码之前将其激活。 We noticed that the PATH variable isn't getting set correctly. 我们注意到PATH变量的设置不正确。 (even though it was set correctly prior to making the image, and is set correctly for all users after SSH'ing into instance) (即使在制作映像之前已正确设置,并且在SSH进入实例后已为所有用户正确设置)

We've tried modifiying the path in the shell script, but nothing is sticking. 我们已经尝试过修改Shell脚本中的路径,但是没有任何残留。 We ran the following code: 我们运行以下代码:

#!/bin/bash
export=/opt/conda/bin:$PATH
which python
which conda
conda activate etl

We checked $PATH before and after running export=/opt/conda/bin (no change). 在运行export=/opt/conda/bin之前和之后,我们检查了$ PATH(没有更改)。 which python returns the wrong python, and which conda returns not found. which python返回错误的python,以及which conda返回未找到。

$PATH before and after is: /sbin:/usr/sbin:/bin:/usr/bin 之前和之后的$ PATH是: /sbin:/usr/sbin:/bin:/usr/bin

After hours of work, the two critical lines needed (regardless of your usage of the Miniconda AMI) is: 经过数小时的工作,无论是否使用Miniconda AMI,所需的两条关键线是:

#!/bin/bash
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc #or the path to your conda.sh
source ~/.bashrc
conda activate <full path to environment>

for some reason, cloud-init ignores all other attempts to modify path 由于某种原因,cloud-init会忽略所有其他尝试修改路径的尝试

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

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