简体   繁体   中英

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). Since the code runs in a conda environment, we are trying to activate it prior to running the Python/Pandas code. We noticed that the PATH variable isn't getting set correctly. (even though it was set correctly prior to making the image, and is set correctly for all users after SSH'ing into instance)

We've tried modifiying the path in the shell script, but nothing is sticking. 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). which python returns the wrong python, and which conda returns not found.

$PATH before and after is: /sbin:/usr/sbin:/bin:/usr/bin

After hours of work, the two critical lines needed (regardless of your usage of the Miniconda AMI) is:

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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