I am currently trying to find a way to activate an anaconda environment within a Python script that is running on a remote server. I have found the following similar questions on Stack Overflow here, and here, but have had no success with what they suggest. I just keep getting this simple error sh: 1: source: not found
.From my understanding this is because the conda environment it not being found After much research, I have found no other way to do this when using a python script, but if anyone has an suggestions I would love to pass some ideas around! Thanks, in advance!
Here is the code:
import os
import subprocess
from subprocess import Popen
value = 'activate punc2'
#os.system("cd /Users/elicobler/tiny_dancer/Testing/thrive/API_text_results")
#os.system('source root/conda3/envs/punc2/bin/activate /root/conda3/envs/punc2')
#os.system('source activate punc2')
#Popen('source activate punc2')
#subprocess.call('source', value)
subprocess.run('source root/conda3/envs/punc2/bin/activate /root/conda3/envs/punc2 && cat /var/www/html/1.22.18 - Monday - Hr 1 - Seg 1-5a65e2698c1dd07f02c25679.txt | python punctuator.py models/endquote.pcl /var/www/html/1.22.18 - Monday - Hr 1 - Seg 1-5a65e2698c1dd07f02c25679.txt && source deactivate', shell=True)
Your default shell doesn't support source
, but it's likely your system also provides bash. Try something like this:
subprocess.run("bash -c 'source /root/conda3/envs/punc2/bin/activate /root/conda3/envs/punc2 && ... && source deactivate', shell=True)
You don't need a shell to invoke a shell, so you could probably get away with something more like
subprocess.run("/use/bin/env", "bash", "-c", "source /root/conda3/envs/punc2/bin/activate /root/conda3/envs/punc2 && ... && source deactivate")