Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Python version is installed in another conda env

Tags:

python

conda

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.

like image 944
flaschbier Avatar asked Apr 02 '17 12:04

flaschbier


People also ask

What version of Python is in conda environment?

Conda treats Python the same as any other package, so it is easy to manage and update multiple installations. Anaconda supports Python 3.7, 3.8, 3.9 and 3.10. The current default is Python 3.9.

How do I change the Python version to an existing conda environment?

Open up your terminal. Search for available versions - can search for what you want, but we'll look for “python” > conda search python which returns something like this: Fetching package metadata: . To change your python version, you can now just type: conda install python=3.5.


2 Answers

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.

like image 101
elomage Avatar answered Sep 17 '22 13:09

elomage


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
like image 28
karlson Avatar answered Sep 18 '22 13:09

karlson