How can I activate Conda environment using python code?

Viewed 272

How can I activate a Conda environment using python code? Here are things I tried so far but they don't seem to change my environment:

import os
os.system('conda activate envname')
# Doesn't work but returns no errors.
import os
stream = os.popen('conda activate envname')
output = stream.read()
# Doesn't work but returns no errors.
import subprocess
process = subprocess.Popen(['conda', 'activate', 'envname'],
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE,
                     shell=True)
stdout, stderr = process.communicate()
# Doesn't work but returns no errors.

So how can I change my Conda environment using python, and I want it to work on all platforms (linux, mac, windows)?

EDIT 1: So from this question it seems that all I'm doing is changing the environment temporarily during the existence of the subprocess. I want a way to change it in my current running shell...

0 Answers
Related