How to Install Python on CentOS 8

29/12/2020
In this article, I am going to show you how to install Python 2 and Python 3 programming language interpreter for coding and running Python programs. So, let’s get started.

Installing Python 3:

You don’t need to do anything in order to install Python 3 on CentOS 8 as it is installed by default.

You can check what version of Python 3 programming language interpreter is installed with the following command:

$ python3 –version

As you can see, at the time of this writing, Python 3.6.8 is the default Python 3 programming language interpreter on CentOS 8.

You can find the path of your Python 3 interpreter with the following command:

$ whereis python3

As you can see, you can access Python 3 as either python3 (/usr/bin/python3), or python3.6m (/usr/bin/python3.6m), or python3.6 (/usr/bin/python3.6) command.

You can also access Python 3 interpreter in path /bin/python3, /bin/python3.7 and /bin/python3.7m

The Python 3 interpreter paths are useful when you write Python 3 scripts. I will talk about that later in this article.

If for some reason, you don’t have Python 3 installed (very unlikely), you can install it with the following command:

$ sudo yum install python3

Installing Python 2:

On CentOS 8, Python 2 is not installed by default. But, you can easily install it from the official package repository of CentOS 8.

To install Python 2 on CentOS 8, run the following command:

$ sudo yum install python2

Now, press Y and then press <Enter> to confirm the installation.

Python 2 should be installed.

Now, run the following command to confirm that Python 2 is installed correctly.

$ python2 –version

As you can see, Python 2.7.15 is installed and working correctly.

You can also find the path of the Python 2 programming language interpreter with the following command:

$ whereis python2

As you can see, the path of the Python 2 interpreter is /usr/bin/python2 and /usr/bin/python2.7

You can also access Python 2 interpreter in path /bin/python2 and /bin/python2.7

The Python 2 interpreter paths are useful when you write Python 2 scripts. I will talk about that later in this article.

Writing Your First Python Script:

Before you start writing a Python script, you must decide what version (2 or 3) of Python programming language you are going to use.

These days, most likely you will want to start your program in the latest version of Python, which is Python 3. Unless you have some specific purpose, you will not use Python 2.

I have created a python script hello.py in the ~/codes directory.

The first line of your Python script should have a shebang! line. This line defines what interpreter to use for running the Python script.

The shebang line starts with #! followed by the path of the Python programming language interpreter.

For Python 3, the shebang line can be either one of the followings:

#!/bin/python3
#!/usr/bin/python3
#!/bin/python3.6
#!/usr/bin/python3.6
#!/bin/python3.6m
#!/usr/bin/python3.6m

For Python 2, the shebang line can be either one of the followings:

#!/bin/python2
#!/usr/bin/python2
#!/bin/python2.7
#!/usr/bin/python2.7

Here is how a shebang line actually looks like in our hello.py script.

You can also use the env command in the shebang line to start the Python programming language interpreter. If you use the env command, then you don’t have to memorize/find out the Python programming language interpreter paths.

The env command is in the path /bin/env and /usr/bin/env, this is all you need to know. I prefer the env style shebang lines.

For Python 3, the shebang line if env is used can be either one of the followings:

#!/bin/env python3
#!/usr/bin/env python3
#!/bin/env python3.6
#!/usr/bin/env python3.6
#!/bin/env python3.6m
#!/usr/bin/env python3.6m

For Python 2, the shebang line if env is used can be either one of the followings:

#!/bin/env python2
#!/usr/bin/env python2
#!/bin/env python2.7
#!/usr/bin/env python2.7

Here is how a shebang line actually looks like in our hello.py script if env command is used.

I wrote a hello world Python 3 script hello.py. Let’s run this Python script.

First, navigate to the ~/codes directory as follows:

$ cd ~/codes

As you can see, hello.py Python script is there.

$ ls -lh

Now, add executable permission to the Python script hello.py as follows:

$ chmod +x hello.py

Now, you can run the Python script as follows:

$ ./hello.py

As you can see, the correct output is printed on the terminal.

So, that’s how you install Python 2/3 programming language on CentOS 8 and write your first Python 2/3 script. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Performance Testing [Phần 3] Hướng dẫn sử dụng Gatling Recoder

Gatling Recorder hỗ trợ người dùng sinh ra kịch bản test nhanh chóng bằng cách thu thập các request HTTP...
30/12/2020

CLI #1: Giới thiệu lệnh nload và lệnh iperf để giám sát tốc độ card mạng

Hôm nay Onet sẽ giới thiệu đến các bạn 2 công cụ dùng để đo lường dữ liệu mạng và hiển...
30/12/2020

[Netbox] [Phần 1] Hướng dẫn cài đặt NetBox trên CentOS 7

NetBox là một ứng dụng web nguồn mở được thiết kế kế để giúp quản lý hạ tầng mạng máy...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022