1
votes

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.

1

1 Answers

1
votes

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.)