Skip to main content

Installing Python on CentOS

Hello people, welcome to our guide on installing Python on CentOS, a Linux distribution known for its stability and reliability. Python is a programming language that's widely used in various fields, from web development to data analysis and machine learning. As CentOS is often favored in enterprise environments, having Python at your disposal can greatly enhance your system's capabilities.

In this tutorial, we'll walk through the process of installing Python on CentOS, ensuring that you have a good Python environment ready for development.

Prerequisites

Before we begin, ensure that you have:.

  • Access to a CentOS-based system with root privileges or the ability to execute commands with sudo.
  • An active internet connection to download Python and its associated packages.

Step 1: Update Your System

It's always a good practice to update your system's package lists and upgrade existing packages to the latest versions. This ensures that you have the most recent security patches and package updates.

sudo yum update -y

After running this command, your system will be up to date, and you're ready to proceed with the Python installation.

Step 2: Check for Existing Python Installations

CentOS comes with Python pre-installed, but it might not be the latest version. Let's check which versions of Python are available on your system:.

python --version
python3 --version

If you have Python 3.x installed, you'll see the version number after running the python3 --version command. If you only have Python 2.x (which is deprecated), it's time to install Python 3.

Step 3: Install Python 3

CentOS 7 and earlier versions come with Python 2.x by default. To install Python 3, you can use the CentOS package manager, yum.

sudo yum install python3 -y

This command will install the latest available Python 3 package from the CentOS repositories.

For CentOS 8, Python 3 is installed by default. However, you can still update it to the latest version using yum.

sudo yum install python3 -y

Step 4: Verify the Installation

After the installation is complete, verify that Python 3 is installed correctly:.

python3 --version

You should see the version of Python 3 that was installed. For example:.

Python 3.6.8

Step 5: Install pip for Python 3

pip is the package installer for Python. You can install pip for Python 3 using the following command:.

sudo yum install python3-pip -y

Once installed, verify that pip is working correctly:.

pip3 --version

You should see the version of pip displayed, along with the path to the Python 3 installation.

Step 6: Using Python Virtual Environments

Virtual environments allow you to manage dependencies for different projects separately. To create a virtual environment for Python 3, you first need to install the python3-virtualenv package:.

sudo yum install python3-virtualenv -y

Now, create a virtual environment in your project directory:.

mkdir ~/my_python_project
cd ~/my_python_project
python3 -m virtualenv myenv

To activate the virtual environment:.

source myenv/bin/activate

Your command prompt should now indicate that you are working inside the virtual environment (e.g., (myenv) [username@hostname ~]$).

To deactivate the virtual environment when you're done:.

deactivate

Step 7: Set Python 3 as the Default Version

If you want to set Python 3 as the default version on your system, you can use the alternatives command:.

sudo alternatives --install /usr/bin/python python /usr/bin/python3 10

After running this command, you can switch between Python versions using:.

sudo alternatives --config python

Select the Python 3 version from the list to set it as the default.

Step 8: Testing Python 3

Let's write a simple Python script to test the installation. Create a file named test_python.py:.

echo "print('Hello, Python on CentOS!')" > test_python.py

Run the script using Python 3:.

python3 test_python.py

You should see the output:.

Hello, Python on CentOS!

Example 2: Simple Calculator

Create a file named calculator.py and add the following code:

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

print("Sum: ", num1 + num2)
print("Difference: ", num1 - num2)
print("Product: ", num1 * num2)
print("Quotient: ", num1 / num2)

To run the script, run the following command:

python3 calculator.py

Enter two numbers when prompted. The script will perform addition, subtraction, multiplication, and division on the two numbers and display the results.

What to Watch Out For

  • Default Python Version: CentOS 7 and earlier use Python 2.x as the default. Make sure to use python3 and pip3 to access Python 3 and its package manager.
  • Permissions: Ensure you have the necessary permissions to install packages. You may need to use sudo or log in as the root user.
  • Path Conflicts: If you encounter issues with multiple Python versions, check your PATH environment variable and the symbolic links managed by alternatives.

You now have a working Python 3 environment on your CentOS system. You can install additional packages using pip, manage project dependencies with virtual environments, and even set Python 3 as the default version for your system.