hello dear phpfreaks

what options do we have to test the python version:

we can run the following commands and whichever gives an output is the version you have:

Python 2: python --version
Python 3: python3 --version

Note: we can have both installed and used independently by simply adding the shebang to the top of
the Python script to indicate which we want to use.

Besides this:
well we can use python -V (et al.) to show you the version of Python that the python command resolves to.
But to see every version of python in your system takes a bit more.

we can check the resolution with readlink -f $(which python).

For example in Ubuntu: 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.

    Is this a question or an article answering the question?

    But if it is a question,

    "How can I know which version of Python I have,"
    You can ask it.

    I went to http://www.python.org/ and the documentation there, and used its search box to look for "version".
    According to the Python documentation on the Python site, the standard library includes "platform", which in turn contains a whole bunch of methods related to the Python version.

    Incidentally, phpfreaks is a different site. This is PHPBuilder. It's not PHPFreaks, it's not a Python site, nor is it a Perl site.
    You're just spamming all your questions to a whole bunch of different forums in the hopes that sometimes someone will answer.

      6 months later
      Write a Reply...