How to check all versions of python installed on osx and centos - python

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 lookup all the version of python installed on centos so I don't accidentally install it twice.

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

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

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

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).

Use, yum list installed command to find the packages you installed.

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.

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).

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.

compgen -c python | grep -P '^python\d'
This lists some other python things too, But hey, You can identify all python versions among them.

Sift through the output of this script.
sudo find / -name 'python*' -type f -exec du -h {} + | sort -r -h ~/Documents/python_locations.txt

ls -l /usr/bin/python* & ls -l /usr/local/bin/python*

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

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

Related

Python Multiple version installation in Linux rocky

I am new to Python and Linux env, so a little confused.
I want to find where my Python3.9 is installed, so that I can update the PATH..
Running python --version or python3 --version give me Python 3.6.8
I want to install Python 3.9, so when I run sudo dnf install -y python39 it gives me
Package python39-3.9.12-1ep1.el8.x86_64 is already installed.
ls /usr/bin/python* doesn't show 3.9
Output of above command - /usr/bin/python /usr/bin/python3 /usr/bin/python3.6
/usr/bin/python3.6m
Also tried alternatives --config python but it shows only 3.6
-----------------------------------------------
* 1 /usr/libexec/no-python
+ 2 /usr/bin/python3
which python3.6 gives /usr/bin/python3.6
which python3.9 gives /usr/bin/which: no python3.9 in....
rpm -ql python39-3.9.12-1ep1.el8.x86_64 gives me
/etc/gdbinit.d/python39.gdb. /opt/python3.9.
/opt/python3.9/bin
I could be missing something obvious, if someone can point me in the right direction it will be awesome.
what happens when you enter the following command: python3 --version and is it any different compared to running python --version?
I am not sure what is your actual requirement is. But if you have already installed python 3.9 in your machine, then you can have this and all previous versions applied to different projects. This is selected when you create virtual environments. For example if you want to run a project that built on 3.6, then you can select python interpreter 3.6 at the time of creation.
So the virtual machine I was working was built using vagrant, it had python 3.6 in usr/bin and python 3.9 in opt/python3.9. Newbie like me who wanna understand what /opt is - https://www.baeldung.com/linux/opt-directory
To search any package used command - rpm -ql python39-3.9.12-1ep1.el8.x86_64
Once package is found add it to path as symlinking binaries can be confusing
vim ~/.bashrc
PATH=/opt/python3.9/bin

How to fix "python version are always returned 2.7 instead of 3.7"

I downloaded python 3.7.3 but python 2.7.10 already existed.
Now python --version returns 2.7.10
How to fix this?
If you are working on Linux, you can always type python3 --version to check if it is installed and be sure it is the version you want to use.
There are several ways to make python call Python 3 by default. For instance you can create an alias. Type whereis python3 so you get the installation path to python3 (normally it is located in /usr/bin/python3). If that's the case you can simply add to ~/.bashrc the following line:
alias python='/usr/bin/python3'
Then, source that file or reload the session. This assumes that /usr/bin/python3 is the location of Python 3. Please, note that other commands that depend on your Python installation (such as pip or coverage) are still pointing to the ones installed by Python 2, so you may want to do the same for them, or make sure that you call pip3 instead of pip if you want to install any extra package.
If you are using linux or macOS, the python command refers to the built-in Python2. You need to use the python3 command to use Python3.
So running:
python3 --version
should give you the expected output.
Also, when you want to run your scripts in Python3, you need to use:
python3 myscript.py
Both the python2x and python3x can exist on a system. On linux machines the default python version is python2x.
so if you want to work with python2x type python2.7 or python3 otherwise.
Installation
sudo apt update
sudo apt install python3.9
Checking the installation with the python3 --version command still returns the old version. To fix this :
Create a list of update alternatives. First, add the old version to the list with the command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
Now add the new version:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
Type the following command to configure the priority status of the versions:
sudo update-alternatives --config python3
check the default version:
python3 --version
DONE

ImportError: No module named psutil while using robotframework

I'm using robotframework for automation. I'm trying to use a method from my python library which needs psutil. The problem is when I execute from the robotframwork it gives error ImportError: No module named psutil but when I execute it with Pycharm I don't get any error. I have installed psutil using pip install psutil . I searched a lot on web but couldn't find any reason for this issue.
My experience with this error was that I had multiple versions of python installed on my ubuntu server 16.04LTS.
My PATH resolved to /usr/local/python -> python 2.7.10 meaning typing python into the commandline I got 2.7.10, but the standard path /usr/bin/python linked to python 2.7.12. The psutil module and others were only installed for version 2.7.10
My resolution was to re-symlink /usr/bin/python to point to my 2.7.10 version:
# Find which python your PATH is pointing at and the version
$ which python
# Returns
/usr/local/bin/python
$ /usr/local/bin/python --version
# Returns
Python 2.7.10
$ /usr/bin/python --version
# Returns
Python 2.7.12
Unlink and relink to correct python version in /usr/bin/python
$ cd /usr/bin
$ sudo unlink python
$ sudo ln -s /usr/local/bin/python python
# check python version points correctly
$ /usr/bin/python --version
# Now correctly returns
Python 2.7.10
P.s. Bear in mind it's still worth checking all your modules are correctly installed now that your environment is pointing at the 1 python version
Could you try setting the PYTHONPATH in your environment or by passing it as an option.
This person had a different issue, but the fix I think will be the same:
setup pythonpath before starting test suite
Cheers,
K

iPython 3 uses Python 2 instead of Python 3

iPython 3 — installed with pip3 and located at /usr/local/bin/ipython3 — runs with Python 2.7.6 in both the console and notebook whereas I want it to run with Python 3 as it should.
This used to work fine before, but I may have reinstalled stuff using MacPorts or HomeBrew that triggered the change.
$ which -a python
/opt/local/bin/python # 2.7.9
/usr/local/bin/python # 2.7.8
/usr/local/bin/python
$ which -a python2
/usr/local/bin/python2 # 2.7.8
/usr/local/bin/python2
$ which -a python3
/usr/local/bin/python3 # 3.2.4
/usr/local/bin/python3
$ pip3 -V
pip 1.5.6 from /usr/local/lib/python3.4/site-packages (python 3.4)
$ ipython3 -V
2.3.1
I edited the shebang on the iPython executable from #!/usr/bin/python to #/usr/local/bin/python3, but I am not sure if this is a hacky or bad solution.
what work for me is changing #!/usr/bin/python to #! /usr/bin/python3 in file /usr/local/bin/ipython3
Just make sure that above address in all files coming out from 'whereis ipython3' should be same.

Mac OSX: Switch to Python 2.7.3

I've installed the Mac OSX Python 2.7.3, and tried switching to the new version:
$ python2.7 -V
Python 2.7.3 # It was installed
$ python -V
Python 2.6.1 # Default is 2.6
$ export VERSIONER_PYTHON_VERSION=2.5
$ python -V
Python 2.5.4 # I can switch to 2.5
$ export VERSIONER_PYTHON_VERSION=2.6
$ python -V
Python 2.6.1 # And 2.6
But:
$ export VERSIONER_PYTHON_VERSION=2.7
$ python -V
python: VERSIONER_PYTHON_VERSION environment variable error (ignored)
Python 2.6.1
Can't switch to 2.7! Why is that?
The python.org install will put a python executable in /usr/local/bin . Apple's python is in /usr/bin
To call the python.org python you can use its full path /usr/local/bin/python or change your path to put /usr/local/bin before /usr/bin. You will still be able to call Apple's python by using its full path /usr/bin/python
Some python installs e.g. Macports provide a tool that provides symbolic links to various python installs and allows you to switch between them.
I don't think we can switch the python version on the Mac OS X without resorting to something like MacPorts or ActivePython.
I think it's because VERSIONER_PYTHON_VERSION is specific to python packages built by apple.

Categories