I have two scripts A.py
and B.py
which runs in different conda environments on my Ubuntu 18.04 machine.
I would like to use A.py
to call B.py
by opening a bash shell, calling "conda activate envB", followed by "python B.py args". However, neither of the attempts below work:
Attempt 1) :
import subprocess
subprocess.call(['conda', 'activate', 'envB'])
for i in range(10):
subprocess.call(['python', 'B.py', i])
I'm getting an error message suggesting conda is not properly set up:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run .....
I tried adding
subprocess.call(['source', '~/.bashrc'])
to setup the environment paths but the shell seems to be complainingNo such file or directory: 'source': 'source'
Attempt 2)
subprocess.call(['/bin/bash', '-i', '-c', "conda activate myenv"])
subprocess.call(['/bin/bash', '-i', '-c', "echo $(which python) >> test.txt"])
for i in range(10):
subprocess.call(['/bin/bash', '-i', '-c', "python B.py -{}".format(i)])
- This doesn't seem to activate myenv. Is there any way to keep the shell open after the first command?
- This also appears to create a background process that continues to run after I've terminated A.py. B.py appears to run in the background but I can't kill it using bash.
Attempt 3)
I also tried using os.system(command)
but can't figure out how to load environment variables with os.system.
Hence this doesn't work either: How to call anaconda environment to run specific package of python(2.7) from other python(3.7) script via os.system()?