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.

Sandclock 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, Sandclock 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

CLI #3: Giới thiệu lệnh chattr để bảo vệ sự toàn vẹn của file

Đối với Linux, thông thường chúng ta sử dụng phân quyền bằng lệnh chmod để bảo vệ tập tin....
30/12/2020

So sánh BigBlueButton và Jitsi : Lựa chọn sao cho đúng?

BigBlueButton và Jitsi đang là hai phần mềm video confenrence mã nguồn mở rất phổ biến và được...
30/12/2020

How to Install Hyper.js Terminal on CentOS 8

Hyper terminal is built with web technologies such as HTML, CSS, JavaScript, Electron.js etc. It looks very modern and...
29/12/2020
Bài Viết

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

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021