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

Cài đặt Chrony trên CentOS/RHEL 7

Ở bài trước chúng ta đã biết được Chrony là một dịch vụ đồng bộ thời gian trên nhiều hệ...
30/12/2020

Nextcloud [Part 6] – Cài đặt App tính năng trên NextCloud

Mục lục : Quản lý App trên NextCloud Cài đặt ứng dụng trên NextCloud 1. Quản lý App trên...
30/12/2020

How to Install CentOS 8 Stream

CentOS 8 Stream is a rolling-release version of the newly released CentOS 8. You will get latest version of softwares and...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024