1

I have a autoscript.py module where I have this code snippet:

import subprocess
subprocess.run('/root/anaconda3/bin/conda info --envs', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

The above code runs fine and gives me a response as follows:

CompletedProcess(args='/root/anaconda3/bin/conda info --envs', returncode=0, stdout=b'# conda environments:\n#\nbase                     /root/anaconda3\nO9PythonUnitTests     *  /root/anaconda3/envs/O9PythonUnitTests\npy36                     /root/anaconda3/envs/py36\n\n')

However, when I modify the above code to activate or deactivate a conda environment as follows:

import subprocess
subprocess.run('/root/anaconda3/bin/conda deactivate', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

I am getting the following conda init <SHELL_NAME> error:

CompletedProcess(args='/root/anaconda3/bin/conda deactivate', returncode=1, stdout=b"\nCommandNotFoundError: Your shell has not been properly configured to use 'conda deactivate'.\nTo initialize your shell, run\n\n    $ conda init <SHELL_NAME>\n\nCurrently supported shells are:\n  - bash\n  - fish\n  - tcsh\n  - xonsh\n  - zsh\n  - powershell\n\nSee 'conda init --help' for more information and options.\n\nIMPORTANT: You may need to close and restart your shell after running 'conda init'.\n\n\n")

I am running the autoscript.py in Ubuntu 18 terminal as follows:

python autoscript.py
halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • Have you tried running the `conda init ` like it's suggested? – Jamie Nov 25 '19 at 09:53
  • yes, I tried `conda init bash`. After this I can run `conda activate` from my terminal. But getting the same error as above when trying to run the same conda activate command via subprocess.run() – user3243499 Nov 25 '19 at 09:56
  • 1
    I had a similar problem once which boiled down to explicitly having to source `/etc/profile.d/conda.sh` in order to be able to use activate/deactivate. Could be the same problem in your case. – cel Nov 25 '19 at 09:57
  • yeah, I have already tried that by looking at the available SO posts. But it is still not working. – user3243499 Nov 25 '19 at 10:03
  • Can you please check if you are also getting the same error message while using `subprocess.run()`? – user3243499 Nov 25 '19 at 10:05
  • This works for me: `subprocess.run('source /etc/profile.d/conda.sh && conda deactivate', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)` – cel Nov 25 '19 at 10:08
  • @cel It says, `source not found`. It tried this: `subprocess.run('source /root/anaconda3/etc/profile.d/conda.sh && /root/anaconda3/bin/conda deactivate', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)`. And it gives me `CompletedProcess(args='source /root/anaconda3/etc/profile.d/conda.sh && /root/anaconda3/bin/conda deactivate', returncode=127, stdout=b'/bin/sh: 1: source: not found\n')`` – user3243499 Nov 25 '19 at 10:18
  • Try replacing `source` with `.` https://stackoverflow.com/questions/4732200/replacement-for-source-in-sh – cel Nov 25 '19 at 10:22
  • Nope. With `.` it is not complaining about `source` but then hitting the same original issue due to the conda deactivate part. – user3243499 Nov 25 '19 at 10:43
  • Possibly related: [Using `conda run`](https://stackoverflow.com/a/58455069/570918). – merv Nov 25 '19 at 18:27

0 Answers0