What version of Python do I have?

Solution 1:

You can use python -V (et al.) to show you the version of Python that the python command resolves to. If that's all you need, you're done. But to see every version of python in your system takes a bit more.

In Ubuntu we can check the resolution with readlink -f $(which python). In default cases in 14.04 this will simply point to /usr/bin/python2.7.

We can chain this in to show the version of that version of Python:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V'
/usr/bin/python2.7: Python 2.7.6

But this is still only telling us what our current python resolution is. If we were in a Virtualenv (a common Python stack management system) python might resolve to a different version:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V'
/home/oli/venv/bin/python: Python 2.7.4

This is real output.

The fact is there could be hundreds of different versions of Python secreted around your system, either on paths that are contextually added, or living under different binary names (like python3).

If we assume that a Python binary is always going to be called python<something> and be a binary file, we can just search the entire system for files that match those criteria:

$ sudo find / -type f -executable -iname 'python*' -exec file -i '{}' \; | awk -F: '/x-executable; charset=binary/ {print $1}' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V'
/home/oli/venv/bin/python: Python 2.7.4
/media/ned/websites/venvold/bin/python: Python 2.7.4
/srv/chroot/precise_i386/usr/bin/python2.7: Python 2.7.3
/srv/chroot/trusty_i386/usr/bin/python2.7: Python 2.7.6
/srv/chroot/trusty_i386/usr/bin/python3.4: Python 3.4.0
/srv/chroot/trusty_i386/usr/bin/python3.4m: Python 3.4.0
/usr/bin/python2.7: Python 2.7.6
/usr/bin/python2.7-dbg: Python 2.7.6
/usr/bin/python3.4: Python 3.4.0
/usr/bin/python3.4dm: Python 3.4.0
/usr/bin/python3.4m: Python 3.4.0
/web/venvold/bin/python: Python 2.7.4

It's obviously a pretty hideous command but this is again real output and it seems to have done a fairly thorough job.

Solution 2:

Type following in the terminal (Ctrl+Alt+T):

python -V

or

python --version

You can find a list of options/parameters for many commands in the terminal by typing the command followed by --help

Example:

python --help

Manual/manpages also available for most of such CLI which can be displayed by man <command> (Ex: man python)

From man python:

COMMAND LINE OPTIONS
        -V ,  --version
              Prints the Python version number of the executable and exits.

There is also python3 installed on many machines, so you can do:

python3 --version

to find out what python 3.x you are running.

Solution 3:

python --version

and

python2 --version

show the version of Python 2.x,

python3 --version

the installed version of Python 3.x

Solution 4:

If you want to see all versions of Python available as commands, run compgen -c python. E.g:

$ compgen -c python | sort -u
python
python2
python2.7
python3
python3.4
python3.4m
python3m

If you want to get the version of each of the above:

compgen -c python | sort -u | grep -v -- '-config$' | while read -r p; do
     printf "%-14s  " "$p"
     "$p" --version
done
python         Python 2.7.6
python2        Python 2.7.6
python2.7      Python 2.7.6
python3        Python 3.4.3
python3.4      Python 3.4.3
python3.4m     Python 3.4.3
python3m       Python 3.4.3

Note: I'm filtering out the python*-config programs with grep -v since they don't support the --version flag. For example:

$ python3-config --version
Usage: /usr/bin/python3-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--abiflags|--configdir