switch conda environment from within python script

Question:

Is it possible at all to launch a Python script using conda environment ENV1 and at some point within the script to switch to environment ENV2 and the code that follows that point to be executed within ENV2 instead of ENV1? I have tried the following suggested solution but it doesn’t work:

https://unix.stackexchange.com/questions/622383/subprocess-activate-conda-environment-from-python-script?newreg=191cf527472141d2a76a244969897af8

Below is an example script. Assuming that I launch the script while having ENV1 as my active environment:

import subprocess

print("Changing Conda virtual environment to 'ENV2'.")
cmd = '. $CONDA_PREFIX_1/etc/profile.d/conda.sh && conda activate ENV2 && echo $CONDA_PREFIX'
subprocess.call(cmd, shell=True, executable='/bin/bash')
print(os.environ['CONDA_PREFIX'])

The only viable solution I could think of is to save all code that occurs after "subprocess.call(cmd, shell=True, executable=’/bin/bash’)" into a separated script named "script_for_ENV2.py" and replace the above script with this:

import subprocess

cmd = 'conda run -n ENV2 script_for_ENV2.py'
subprocess.call(cmd, shell=True, executable='/bin/bash')
Asked By: tevang

||

Answers:

Is it possible at all to launch a Python script using conda environment ENV1 and at some point within the script to switch to environment ENV2 and the code that follows that point to be executed within ENV2 instead of ENV1?

No, this isn’t possible, at least not at a practical level, since launching a python script creates a process running in the underlying operating system.

One possibility is that you can launch another python script using whatever conda environment you want. But why do you want to do this?

Answered By: Code-Apprentice

I’ve tried looking into this… as I had a similar problem. Here’s what I’ve found.

  1. using os.system("activate < env-name >") and any subprocess or system command after seems to performed in that env.

  2. This is the path I went with since I wanted to visually see the commands being done in that environment (performed on windows from python script) Maybe some sort of this method could be incorporated on linux? I’m not too sure about this but I will test it out and add to this if it was possible or if I find another solution. Nonetheless,
    using this for loop I was able to run/append as many commands on the separate window/environment. One can also replace the "cmd /k" with "cmd /c" to hide the command prompt.
    I hope one of these methods helps or provides you with a path.
    best,
    -G

    my_env=[]
    cmdStr=""
    my_env.append("echo " Running Command "")
    my_env.append("activate <env-path or name>")
    my_env.append(" echo " do stuff on this env"")
    for ii in my_env:
        cmdStr = cmdStr + ii + " ^&^& "
    os.system("start " + cmdStr + "cmd /k")
    
Answered By: Everydaycoder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.