Activate conda environment using subprocess

Question:

I am trying to find version of pandas:

def check_library_version():
    print("Checking library version")
    subprocess.run(f'bash -c "conda activate {ENV_NAME};"', shell=True)
    import pandas
    pandas.__version__

Desired output:
1.1.3

Output:

Checking library version

CommandNotFoundError: Your shell has not been properly configured to
use ‘conda activate’. To initialize your shell, run

$ conda init <SHELL_NAME>

Currently supported shells are:

  • bash
  • fish
  • tcsh
  • xonsh
  • zsh
  • powershell

See ‘conda init –help’ for more information and options.

IMPORTANT: You may need to close and restart your shell after running
‘conda init’.

To clarify, I don’t seek to update the environment of the currently running script; I just want to briefly activate that environment and find out which Pandas version is installed there.

Asked By: Isha Nema

||

Answers:

This doesn’t make any sense at all; the Conda environment you activated is terminated when the subprocess terminates.

You should (conda init and) conda activate your virtual environment before you run any Python code.

If you just want to activate, run a simple Python script as a subprocess of your current Python, and then proceed with the current script outside of the virtual environment, try something like

subprocess.run(f"""conda init bash
    conda activate {ENV_NAME}
    python -c 'import pandas; print(pandas.__version__)'""",
    shell=True, executable='/bin/bash', check=True)

This just prints the output to the user; if your Python program wants to receive it, you need to add the correct flags;

check = subprocess.run(...whatever..., text=True, capture_output=True)
pandas_version = check.stdout

(It is unfortunate that there is no conda init sh; I don’t think anything in the above depends on executable='/bin/bash' otherwise. Perhaps there is a way to run this in POSIX sh and drop the Bash requirement.)

Answered By: tripleee

The above didn’t work for me.
What did work is:

import subprocess
output = subprocess.run('''python -c "import sys; print(sys.executable)"
                  source <your-conda-base-dir>/etc/profile.d/conda.sh
                  conda activate <name-of-the-conda-environment-you-want-to-activate>
                  python -c "import sys; print(sys.executable)"
                  conda env export''', executable='/bin/bash', shell=True, capture_output=True)
print(bytes.decode(output.stdout))

Substitute <your-conda-base-dir> by the output of which conda executed in a the interactive terminal you normally use for executing conda commands, but remove /condabin/conda from the end of the output you get, such that the /etc/profile.d/conda.sh location will be found when the above subprocess.run() is executing.

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.