18

How can I find out which Python version is installed in a conda environment where I know the name, but do not want to activate that environment?

Background: I have chosen the lazy way to get python3.6 on my Ubuntu 14.04 machine and installed conda. But I would like to add certain directories to my PYTHONPATH depending on whether the environment has python2.x or python3.x and I plan to do this by wrapping conda's activate like so:

PYMAJOR=$(a_miracle_occurs $CONDAENV)
BASHRC=$(cat <<EOF
source ~/.bashrc
source activate $CONDAENV
export PATH=...
export PYTHONPATH=".../modules$PYMAJOR"
"
EOF
)
bash --rcfile <(echo "${BASHRC}")

I have no compatibility issues with subversions of python and I do not want to setup.py develop the modules in the PYTHONPATH-to-be because there are still too many changes, also on structural level.

Remark: I am aware of conda list -n ENVNAME but this would involve parsing human readable output and I would feel better off to have something retrieving the info in machine readable form.

4 Answers 4

20

Here is a one-liner that will print the environments and the associated python versions:

conda env list | grep -v "^$\|#" |awk '{print $1;}'|xargs -I{} -d "\n" sh -c 'printf "Env: {}\t"; conda list -n {} |grep "^python\s";'

Here is a sample output:

Env: base   python                    2.7.14              h1571d57_29  
Env: python37   python                    3.7.0                hc3d631a_0  

Rationale: Get the list of environments with conda env list, exclude empty lines and #, parse, print the environment packages with conda list -n <env> and grep for python.

You are welcome to adapt the formatting to your liking.

4
  • 1
    Hi. And how would I get python versions of the various conda environments on Windows? Thank you.
    – Confounded
    Commented Nov 1, 2019 at 14:12
  • 1
    With formatted output @eiomage one liner would be: conda env list | grep -v "^$\|#" | awk '{print $1;}' | xargs -I{} -d "\n" sh -c 'printf %-20s "{}"; conda list -n {} | grep "^python\s"'.
    – f0nzie
    Commented Dec 6, 2019 at 23:12
  • 3
    On OSX (which has an older version of xargs) the above will fail. This command works: conda env list | grep -v "^$\|#" |awk '{print $1;}'|xargs -I{} -n 1 sh -c 'printf "Env: {}\t"; conda list -n {} |grep "^python\s";'
    – xavriley
    Commented May 5, 2022 at 13:14
  • @xavriley I gather you just removed -d '\n'?
    – drevicko
    Commented Jun 29, 2022 at 1:29
3

With referencing elomage's answer, I have written a powershell command with the same behaviour.

conda env list | Select-String -Pattern  "^$|#" -NotMatch | ForEach-Object{($_ -split "\s+")[0]} | %{ "Env: {0}`t{1}" -f $_, (conda list -n $_ | Select-String -Pattern "^python\s") }
2

I am not at all familiar with anaconda and everything that follows is a wild guess. If anaconda uses virtualenv internally, the virtualenv should be installed into some directory (maybe something like $ANACONDA_HOME/envs/$CONDAENV?).

If that's the case, then the Python version should be retrievable by simply running $ANACONDA_HOME/envs/$CONDAENV/bin/python --version.

EDIT to address OPs comment:

To only return the version string try:

$ANACONDA_HOME/envs/$CONDAENV/bin/python -c 'import platform; print(platform.python_version())'
>>> 3.6.0
2
  • 1
    $ ~/miniconda2/envs/$CONDAENV/bin/python --version prints Python 3.6.1 :: Continuum Analytics, Inc. which would still require parsing, but I can add a mini inline script with -c '...' which stdout.writes the sys.version_info.major. Great guess :)
    – flaschbier
    Commented Apr 2, 2017 at 13:08
  • for conda 4.7.12, $ ANACONDA_HOME/bin/python --version works Commented Oct 28, 2019 at 10:50
0

Here's my attempt at a one-liner:

cenvs=$(conda env list | grep -ve '^#'); paste <(echo "$cenvs" | awk '{printf "%-15s\n", $1}') <(echo "$cenvs" | awk '
{print $2}' | xargs -I {} sh -c '{}/bin/python --version')

unlike @elomage's answer, this one only calls conda list once, instead of once per environment, which speeds up things tremendously.

It works by taking the path to each environment printed by conda env list and appending bin/python and executing that python binary with the --version flag.

It's a bit gnarly, but gets the job done fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.