93

I just started setting up a CentOS server today and noticed that the default version of Python on CentOS is set to 2.6.6. I want to use Python 2.7 instead. I googled around and found that 2.6.6 is used by system tools such as YUM so I should not tamper with it. Then I opened up a terminal on my Mac and found that I had Python 2.6.8 and 2.7.5 and 3.3.3 installed.

Sorry for the long story. In short I just want to know how to look up all the versions of Python installed on CentOS so I don't accidentally install it twice.

Melebius
  • 6,183
  • 4
  • 39
  • 52
hch
  • 6,030
  • 8
  • 20
  • 24

14 Answers14

161

The more easy way its by executing the next command:

ls -ls /usr/bin/python*

Output look like this:

/usr/bin/python           /usr/bin/python2.7        /usr/bin/pythonw
/usr/bin/python-config    /usr/bin/python2.7-config /usr/bin/pythonw2.7
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
Gabriel Caceres
  • 1,719
  • 1
  • 9
  • 2
  • 2
    This answer is not very specific, please edit it to clarify the intention. – Maurice Aug 23 '16 at 12:44
  • 1
    This one did it for me. which python etc only give me the current version in use, not the other installed ones. – dorien Feb 28 '17 at 05:30
  • 41
    Careful, some could be installed in usr/local too. So run this too. ls -ls /usr/local/bin/python* – OutOnAWeekend Dec 05 '17 at 20:17
  • With this answer i can see all the python versions installed. works perfect ubuntu 17.04 – Phares Feb 20 '18 at 07:26
  • 2
    For OSX users, python2.7 comes pre-installed in /usr/bin/ but any python versions that were downloaded and installed by a user are likely in /usr/local/bin/ (see OutOnAWeekend's comment). – David Skarbrevik Oct 12 '18 at 22:10
  • 1
    This certainly did not give me the python 3.6.8 version installed on my system. – ayounis90 Jan 16 '19 at 23:11
  • 2
    This would also miss anything in anaconda. To me, it sounds like the original question is how do you do this for your entire system, not just in usr/bin – Otherness Apr 12 '20 at 01:43
  • 1
    Why is this comment favored above the one recommending "whereis python"? The whereis will provide far more details and is the standard for finding executable files installed on your system. – Biosopher Sep 23 '22 at 23:29
  • What are the `-ls` flags intended to do in this case? The output looks like a plain `ls` which should be sufficient in this case anyway. – Melebius Jun 13 '23 at 07:10
30

we can directly use this to see all the pythons installed both by current user and the root by the following: whereis python

Ke Li
  • 391
  • 3
  • 5
17

Here is a cleaner way to show them (technically without symbolic links). This includes python2 and python3 installs:

ls -1 /usr/bin/python* | grep '.*[2-3]\(.[0-9]\+\)\?$'

Where grep filters the output of ls that that has that numeric pattern at the end ($).

Or using find:

find /usr/bin/python* ! -type l

Which shows all the different (!) of symbolic link type (-type l).

ejkitchen
  • 559
  • 6
  • 11
danbros
  • 181
  • 1
  • 3
15

Find out which version of Python is installed by issuing the command python --version: $ python --version Python 2.7.10

If you see something like this, Python 2.7 is your default version. You can also see if you have Python 3 installed:

$ python3 --version
Python 3.7.2

If you also want to know the path where it is installed, you can issue the command "which" with python and python3:

$ which python
/usr/bin/python

$ which python3
/usr/local/bin/python3
KPandian
  • 1,148
  • 9
  • 8
7

Use,

yum list installed
command to find the packages you installed.
lpsandaruwan
  • 790
  • 2
  • 11
  • 27
  • It doesn't give the list of python installed in the system. SO want python versions, not the list of packages. The @gabriel answer is good according to me. You can also use "whereis python" . it also gives the list of python on my centos and Mac system. – Arayan Singh Dec 12 '18 at 12:26
  • 1
    In that case you can do `yum list installed | grep python` – lpsandaruwan Dec 13 '18 at 13:03
  • Bro already has done. Now it is showing list python packages NOT list of python versions – Arayan Singh Dec 13 '18 at 13:19
7

COMMAND: python --version && python3 --version

OUTPUT:

Python 2.7.10
Python 3.7.1

ALIAS COMMAND: pyver

OUTPUT:

Python 2.7.10
Python 3.7.1

You can make an alias like "pyver" in your .bashrc file or else using a text accelerator like AText maybe.

Nikaido
  • 4,443
  • 5
  • 30
  • 47
Alan O'Meara
  • 79
  • 1
  • 1
  • 1
    This assumes that the user knows they have python2.x and python3.x. What if they have anaconda and want to see those versions? This isn't general enough be be helpful. – Otherness Apr 12 '20 at 01:40
4

As someone mentioned in a comment, you can use which python if it is supported by CentOS. Another command that could work is whereis python. In the event neither of these work, you can start the Python interpreter, and it will show you the version, or you could look in /usr/bin for the Python files (python, python3 etc).

jm13fire
  • 185
  • 1
  • 7
4
compgen -c python | grep -P '^python\d'

This lists some other python things too, But hey, You can identify all python versions among them.

Appaji Chintimi
  • 613
  • 2
  • 7
  • 19
3

It depends on your default version of python setup. You can query by Python Version:

python3 --version //to check which version of python3 is installed on your computer
python2 --version // to check which version of python2 is installed on your computer
python --version // it shows your default Python installed version.
nurealam siddiq
  • 1,567
  • 10
  • 9
2

To find what versions of python are installed use a whereis command.

$ whereis python | tr ' ' '\n' | grep ^/ | sort

this will show not only system versions installed in /usr directory but also versions installed by pyenv tool.

/etc/python2.7
/etc/python3.10
/etc/python3.8
/etc/python3.9
/home/user/.pyenv/shims/python
/home/user/.pyenv/shims/python3.11
/home/user/.pyenv/shims/python3.11-config
/home/user/.pyenv/shims/python3.9
/usr/bin/python
/usr/bin/python2.7
/usr/bin/python3.10
/usr/bin/python3.10-config
/usr/bin/python3.8
/usr/bin/python3.8-config
/usr/bin/python3.9
/usr/bin/python3.9-config
/usr/include/python3.10
/usr/include/python3.8
/usr/include/python3.9
/usr/lib/python2.7
/usr/lib/python3.10
/usr/lib/python3.8
/usr/lib/python3.9
/usr/local/bin/python3.6
/usr/local/bin/python3.6m
/usr/local/bin/python3.6m-config
/usr/local/lib/python2.7
/usr/local/lib/python3.10
/usr/local/lib/python3.6
/usr/local/lib/python3.8
/usr/local/lib/python3.9
/usr/share/info/python3.9
/usr/share/python
mx0
  • 6,445
  • 12
  • 49
  • 54
1

Sift through the output of this script.

sudo find / -name 'python*' -type f  -exec du -h {}  + | sort -r -h ~/Documents/python_locations.txt
tsm
  • 419
  • 4
  • 11
1
ls -l /usr/bin/python* & ls -l /usr/local/bin/python*
george mano
  • 5,948
  • 6
  • 33
  • 43
1

I would add to @nurealam siddiq answer,

python --version // it shows your default Python installed version.

python2 --version // to check which version of python2 is installed 

python3 --version //to check which version of python3 is installed 

python3.X --version // to further check which python3.X is installed

Singh
  • 504
  • 4
  • 15
0

To check python versions installed in your OS you can run the below commands:-

python2 -version
python3 -version
Jatin
  • 91
  • 5