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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With