Skip to content

Instantly share code, notes, and snippets.

@alexrashed
Created May 26, 2023 10:57
Show Gist options
  • Save alexrashed/d0a2d83d7933d34e18b80df27fefb3d5 to your computer and use it in GitHub Desktop.
Save alexrashed/d0a2d83d7933d34e18b80df27fefb3d5 to your computer and use it in GitHub Desktop.
Conda Multi-Env PoC

activate conda in shell with this command

eval "$(/home/localstack/anaconda3/bin/conda shell.bash hook)"

configure bioconda

conda config --add channels defaults
conda config --add channels bioconda
conda config --add channels conda-forge
conda config --set channel_priority strict

create the parent env

conda create --name parent-env -y

create child env

conda create --name child-env simba -y

run the parent script

conda run -n parent-env python ./parent.py

Output:

['./parent.py']
simba is not available in the env
['./child.py']
We just imported simba. In the current env, we have simba!
import sys
# print the script
print(sys.argv)
# verify that we have simbda here
import simba as si
print("We just imported simba. In the current env, we have simba!")
import sys
import subprocess
# print the script
print(sys.argv)
# verify that simba is not available in the parent env
try:
import simba as si
raise Exception("We shouldn't get here. The parent env does not have simba!")
except ImportError:
print("simba is not available in the env")
# flush the output to make sure that the output is in order
sys.stdout.flush()
# create the command
# - first initialize the shell (this might be done automatically, depending on your environment, this is quite specific to the OS / system)
# - execute the child.py script in its own conda environemtn
command = """
. <path-to-anaconda>/etc/profile.d/conda.sh &&
conda run -n child-env python ./child.py
"""
# run the command
# - use "shell=True" to execute the command as a shell command string
# - use "env={}" to make sure we start in a clean environment
# - conda seems to pollute the environment quite a lot with path and other environment modiciations
# (https://github.com/conda/conda/issues/11174 and others)
subprocess.run(command, shell=True, env={})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment