How to Install Python 3 on CentOS 7

Introduction

Before we begin talking about how to install Python 3 on CentOS 7, let's briefly understand – What is Python?

Python 3 is the latest version of the Python programming language. With its extensive library, Python 3 is widely used for web development, data analysis, artificial intelligence, and automation. Its simplicity and readability make it a popular choice for beginners.

Python has been used in the development of many well-known applications and websites, including YouTube, Dropbox, Reddit, Quora, Instagram, and Pinterest.

Python 2 support has been discontinued since 2020. Python 3 is the language of the present and the future.

In this tutorial, you will install Python 3 on CentOS 7. We will also address a few FAQs on how to install Python 3 on CentOS 7.

Advantages of Python

  1. Readability: Python's simple syntax enhances code readability and reduces the learning curve.
  2. Versatility: Python can be used for web development, data analysis, scientific computing, AI, and automation.
  3. Extensive library: Python's vast library offers pre-built modules for various tasks, saving development time.
  4. Community support: Python has a large and active community, providing helpful resources and solutions.
  5. Easy integration: Python seamlessly integrates with other languages, facilitating code reuse and cross-platform compatibility.

Enable Software Collections (SCL)

Software Collections, often known as SCL, is a community effort that enables you to create, install, and use various software versions on the same system without affecting the system default packages. You can access newer versions of programming languages and services that are not included in the core repository by enabling SCL.

Python 2.7.5, which is a crucial component of the CentOS base system, is included in CentOS 7. SCL enables you to install newer versions of Python 3.x alongside the default Python v2.7.5, ensuring that the system functions like yum continue to function properly.

You must install the CentOS SCL release file to enable SCL. It is available in the CentOS extras repository and may be installed by running the following command:

sudo yum install centos-release-scl

Installing Python 3 on CentOS 7

You can now install any Python 3.x version you require now that you have access to the SCL repository. The following Python 3 collections are readily available at the moment:

  • Python 3.3
  • Python 3.4
  • Python 3.5
  • Python 3.6

In this article, we will install Python 3.6, which is the most recent version available at the time of writing. To achieve this, use the command shown below into your CentOS 7 terminal:

sudo yum install rh-python36

Using Python 3

After installing the rh-python36 package, you may check the Python version by typing:

python --version
Output

Python 2.7.5

On your current shell, you will observe that Python 2.7 is the default Python version.

To use Python 3.6, launch a new shell instance with the Software Collection scl tool:

scl enable rh-python36 bash

The command above invokes the script /opt/rh/rh-python36/enable, which modifies the shell environment variables.

If you look at the Python version again, you will see that Python 3.6 is now the default in your current shell.

python --version
Output

Python 3.6.3

It is vital to note that Python 3.6 is only configured as the default Python version in this shell session. If you end the session or begin a new session from another terminal, Python 2.7 will be the default Python version.

Installing Development Tools

Python modules must be built using development tools. To install the required tools and libraries, enter:

sudo yum groupinstall 'Development Tools'

Creating a Virtual Environment

Instead of installing Python modules globally, you can use Python Virtual Environments to install them in a separate location for a given project. You will not have to worry about harming other Python projects this way.

With Python 3, using the venv command is the preferred method for creating a new virtual environment.

Suppose you wish to build a new Python 3 project with the name my_new_project inside your user home directory and matching virtual environment.

To begin, create the project directory and switch to it:

mkdir ~/my_new_project
cd ~/my_new_project

With the scl tool, enable Python 3.6:

scl enable rh-python36 bash

Run the following command from the project root to establish a virtual environment with the name my_project_venv:

python -m venv my_project_venv

Type the following to activate the virtual environment and start using it:

source my_project_venv/bin/activate

After you activate the environment, the shell prompt will be prefixed with the name of the environment:

(my_project_venv) user@host:~/my_new_project$

From Python 3.4, when creating virtual environments, pip, the Python package manager, is installed by default.

FAQs to Install Python 3 on CentOS 7

How do I check if Python 3 is installed on CentOS 7? 

Open the terminal and type python3 --version. If Python 3 is installed, it will display the version number. If not, you will need to install it.

Can I have both Python 2 and Python 3 on CentOS 7? 

Yes, you can have both Python 2 and Python 3 installed on CentOS 7 without any conflicts. They are designed to coexist, allowing you to use them as necessary.

How do I switch to Python 3 as the default version on CentOS 7?

To make Python 3 the default version on CentOS 7, you can create an alias by running alias python=python3 in the terminal. This ensures that python always refers to Python 3.

How can I install Python packages using pip with Python 3 on CentOS 7? 

You can install Python packages using pip for Python 3 by using the command pip3 install <package-name>. This ensures that the package is installed for Python 3 specifically.

Can I upgrade Python 2 to Python 3 on CentOS 7?

Python 2 and Python 3 are separate installations. You can have both versions on CentOS 7 without upgrading Python 2. However, you can install Python 3 alongside Python 2 and make it the default version for your scripts.

Are there any compatibility issues between Python 2 and Python 3 on CentOS 7? 

While there are some differences in syntax and features between Python 2 and Python 3, most code written for Python 2 can be migrated and run on Python 3 with minor modifications. However, certain libraries may have version-specific compatibility.

How can I uninstall Python 3 from CentOS 7? 

If you decide to uninstall Python 3, you should exercise caution as it may disrupt system functionality. It's not recommended to remove it. However, if necessary, consult CentOS documentation for guidelines on uninstalling specific packages.

Conclusion

Now that the Python 3 programming environment has been installed on your CentOS 7 system, you may begin working on your Python 3 project.

You may also learn how to install Odoo 11 on CentOS 7 and see an example of how to use the Python 3 virtual environment to run Python software on a CentOS 7 server.

If you have any suggestions or queries, kindly leave them in the comments section.