Python on CentOS

Installing Python on CentOS

Tested on:

Pre Requirements

Steps

  1. Install EPEL repository first.
      $ sudo yum install epel-release
    
  [stephtan@centos6 ~]$ sudo yum install epel-release
  Loaded plugins: fastestmirror
  Setting up Install Process
  Loading mirror speeds from cached hostfile
  * base: mirror.umd.edu
  * extras: mirror.cc.columbia.edu
  * updates: mirror.centos.iad1.serverforge.org
  Resolving Dependencies
  --> Running transaction check
  ---> Package epel-release.noarch 0:6-8 will be installed
  --> Finished Dependency Resolution

  Dependencies Resolved

  ======================================================================================
  Package                  Arch               Version         Repository          Size
  ======================================================================================
  Installing:
  epel-release             noarch             6-8             extras              14 k

  Transaction Summary
  ======================================================================================
  Install       1 Package(s)

  Total download size: 14 k
  Installed size: 22 k
  Is this ok [y/N]:

Hit yes.

  Is this ok [y/N]: y
  Downloading Packages:
  epel-release-6-8.noarch.rpm                                    |  14 kB     00:00
  Running rpm_check_debug
  Running Transaction Test
  Transaction Test Succeeded
  Running Transaction
    Installing : epel-release-6-8.noarch                                            1/1
    Verifying  : epel-release-6-8.noarch                                            1/1

  Installed:
    epel-release.noarch 0:6-8

  Complete!
  [stephtan@centos6 ~]$
  1. install python-pip
  $ sudo yum -y install python-pip

Demo:

  [stephtan@centos6 ~]$ sudo yum -y install python-pip
  Loaded plugins: fastestmirror
  Setting up Install Process
  Loading mirror speeds from cached hostfile
  epel/metalink                                                  |  16 kB     00:00
  * base: mirror.umd.edu
  * epel: ewr.edge.kernel.org
  * extras: mirror.cc.columbia.edu
  * updates: mirror.vtti.vt.edu
  epel                                                           | 4.7 kB     00:00
  epel/primary_db                                                | 5.2 MB     00:06
  Resolving Dependencies
  --> Running transaction check
  ---> Package python-pip.noarch 0:7.1.0-2.el6 will be installed
  --> Processing Dependency: python-setuptools for package: python-pip-7.1.0-2.el6.noarch
  --> Running transaction check
  ---> Package python-setuptools.noarch 0:0.6.10-4.el6_9 will be installed
  --> Finished Dependency Resolution

  Dependencies Resolved

  ======================================================================================
  Package                   Arch           Version                  Repository    Size
  ======================================================================================
  Installing:
  python-pip                noarch         7.1.0-2.el6              epel         1.5 M
  Installing for dependencies:
  python-setuptools         noarch         0.6.10-4.el6_9           base         336 k

  Transaction Summary
  ======================================================================================
  Install       2 Package(s)

  Total download size: 1.9 M
  Installed size: 8.1 M
  Downloading Packages:
  (1/2): python-pip-7.1.0-2.el6.noarch.rpm                       | 1.5 MB     00:02
  (2/2): python-setuptools-0.6.10-4.el6_9.noarch.rpm             | 336 kB     00:00
  --------------------------------------------------------------------------------------
  Total                                                 710 kB/s | 1.9 MB     00:02
  warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
  Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
  Importing GPG key 0x0608B895:
  Userid : EPEL (6) <epel@fedoraproject.org>
  Package: epel-release-6-8.noarch (@extras)
  From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
  Running rpm_check_debug
  Running Transaction Test
  Transaction Test Succeeded
  Running Transaction
    Installing : python-setuptools-0.6.10-4.el6_9.noarch                            1/2
    Installing : python-pip-7.1.0-2.el6.noarch                                      2/2
    Verifying  : python-setuptools-0.6.10-4.el6_9.noarch                            1/2
    Verifying  : python-pip-7.1.0-2.el6.noarch                                      2/2

  Installed:
    python-pip.noarch 0:7.1.0-2.el6

  Dependency Installed:
    python-setuptools.noarch 0:0.6.10-4.el6_9

  Complete!
  [stephtan@centos6 ~]$

Adding python tools to CentOS

This is to have pip to install python packages

yum install python-devel
yum install python-setuptools

Reference: stackoverflow - bash-pip-command-not-found

# install EPEL repository first
$ sudo yum install epel-release
# install python-pip
$ sudo yum -y install python-pip

medium - how-to-install-pip-on-centos-6-8

Viewing python version

View version of python

[stephtan@centos6 bin]$ python -V
Python 2.6.6
[stephtan@centos6 bin]$

Viwing python version directory:

[stephtan@centos6 bin]$ which python
/usr/bin/python
[stephtan@centos6 bin]$

askubuntu: how-to-find-python-installation-directory-on-ubuntu

Viewing all other installs of python:

[stephtan@centos6 bin]$ ls | grep python
python
python2
python2.6
python2.6-config
python-config
[stephtan@centos6 bin]$

Installing python 2.7 on centos 6:

tecadmin - install-python-2-7-on-centos-rhel

  1. Prereq: Firstly make sure that you have GCC package installed on your system. Use the following command to install GCC if you don’t have it installed.
      $ sudo yum install gcc openssl-devel bzip2-devel
    
  2. Download Python 2.7
      $ cd /usr/src
      $ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
    
  3. Extract downloaded archive using tar command.
      $ tar xzf Python-2.7.18.tgz
    
  4. Install Python 2.7
      $ cd Python-2.7.18
      $ ./configure --enable-optimizations
      $ make altinstall
    
  5. Check python version
      /usr/local/bin/python2.7 -V
    

Update bash to reference python 2.7: view other section in this document regarding bash

  1. install pip
      $ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
      $ python2.7 get-pip.py
    

How to reference different pip and python versions

Stack Overflow - dealing-with-multiple-python-versions-and-pip

$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

Installing python3:

liquidweb - how-to-install-python-3-on-centos-7

Update Environment:

$ yum update -y

Install python3

$ yum install -y python3

Verify install:

$ python3

Installing python3

liquidweb - how-to-install-python-3-on-centos-7

Update Environment:

$ yum update -y

Install python3

$ yum install -y python3

Verify install:

$ python3

TODO: Add how to use pip

Installing pip

Reference: liquidweb - how-to-install-pip-on-centos-7

Install pip:

yum -y update
yum -y install python3-pip

To add dependencise use the version of pip installed example:

pip3 install --user -U python-docx==0.8.10

Compiling python pip from source

pypi - pip

How to install: pypa - installing

bootstrap.pypa - bootstrap.pypa

File: bootstrap.pypa - get-pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Specify which python to add pip to

python2.7 get-pip.py

May have to update setuptools prior to installing packages via pip

$ pip2.7 install -U setuptools

Installing wget

Needed to get pip repository

cyberciti - yum-install-wget-redhat-cetos-rhel-7

Install pip security

stackoverflow - insecureplatformwarning-a-true-sslcontext-object-is-not-available-this-prevent

Creating Python Virtual Environment

TODO: VERIFY THIS SECTION

stackoverflow - use-different-python-version-with-virtualenv

Just use the –python (or short -p) option when creating your virtualenv instance to specify the Python executable you want to use, e.g.:

virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>

Since Python 3, the Python Docs suggest creating the virtual environment with the following command:

python3 -m venv <myenvname>

Please note that venv does not permit creating virtual environments with other versions of Python. For that, install and use the virtualenv package.

Journal