1

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 complaining No 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()?

matohak
  • 535
  • 4
  • 19
  • What version of conda are you using and what OS are you running this on? – alex Jun 07 '19 at 14:34
  • conda 4.6.8, Ubuntu 18.04 if that matters. – matohak Jun 07 '19 at 14:38
  • Per https://stackoverflow.com/questions/47246350/conda-activate-not-working, I'd try executing that minus the `conda` - i.e. `activate envB`. – alex Jun 07 '19 at 14:39
  • conda activate is working. `subprocess.call(['/bin/bash', '-i', '-c', "echo $(which python) >> test.txt"])` is printing the correct python path to test.txt. But somehow it freezes my computer. – matohak Jun 07 '19 at 14:41

0 Answers0