Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Install Python On Linux (Ubuntu) Systems

Linux systems are built for programming, so most Linux distributions (Ubuntu, CentOS, etc.) come with Python by default. Some Linux distributions even come with two versions of Python. For example, the latest version of Ubuntu comes with Python 2.x and Python 3.x.

Open the Terminal built into the Linux distribution and enter the python command to detect whether Python is installed and which version is installed, as shown below:
[www.iditect.com@localhost ~]$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
It can be seen that the python command can run normally and output the version information of Python, which indicates that the current Linux distribution already comes with Python 2.7.5.

In addition, the Python command prompt >>> appears at the end of the execution result , which means that we have entered the Python interactive programming environment, where we can directly enter the code and view the running results, as shown below:
[www.iditect.com@localhost ~]$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("iditect URL is: http://www.iditect.com")
iditect URL is: http://www.iditect.com
>>> a=100
>>> b=4
>>> a*b
400
>>> exit()
[www.iditect.com@localhost ~]$
exit() is used to exit the Python programming environment and return to the Linux command line.

Most Linux distributions come with Python 2.x, but not necessarily Python 3.x. To check whether the current Linux distribution has Python 3.x installed, you can enter the python3 command in Terminal, as shown below:
[www.iditect.com@localhost ~]$ Python3
Python 3.6.4 (default , Nov 18 2018 , 13:02:36)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
If the python3 command runs successfully and the Python prompt >>> appears , it means that the current Linux distribution has installed the Python 3 development environment. Just execute the python3 command to start the Python 3 development environment.

If your current Linux distribution doesn't have Python 3 installed, or you feel that your existing Python 3 version is not up to date, then you need to update your Python version. In this section, we use Ubuntu as an example to demonstrate.

Update Python version

Execute the following two commands in the Ubuntu terminal to update the Python version:
$ sudo apt-get update
$ sudo apt-get install python3.8
Explanation of the command:
  • The first command is used to specify to update the source addresses listed in /etc/apt/sources.list and /etc/apt/sources.list.d, so as to ensure the latest installation package.
  • The second command is used to specify the installation of Python 3.8, which is the latest Python version.

Wait for the execution of the above two commands to complete, enter the python3 command in the terminal again , and you can see that the Python interactive programming environment has been updated to Python 3.10.

Reinstall Python

The above update method is only valid if Python has been installed in Ubuntu. If there is no Python environment in your Ubuntu, or you want to reinstall it, then you can download the source code from the official website and compile it yourself.

1) Download the source code

Python official download address: https://www.python.org/downloads/

Open the link, you can see the various versions of Python:



Click the version number in the above picture or the "Download" button to enter the download page of the corresponding version, scroll to the end You can see the Python installation package for each platform.



Right-click the "Gzipped source tarball" and select "Copy Link Address" from the pop-up menu to get the source code package address in .tgz format.

Then execute the following command:
$ wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz
Unzip the source package:
$ tar -zxvf Python-3.10.2.tgz

2) Compile

Compile with the make tool:
$ ./configure --prefix=/usr/local
$ make && sudo make install
Here --prefix=/usr/local is used to specify the installation directory. If not specified, the default installation directory will be used.

After the above commands, we have installed Python. At this point, you can enter the terminal, enter the Python command, and verify whether the installation is successful.

Tips

The python command invokes the Python 2.x development environment by default. If you are accustomed to using Python 3.x and feel that it is a bit troublesome to enter the python3 command every time, you can modify the configuration so that the python command calls the Python 3.x development environment instead. The specific commands are as follows:
$ sudo unlink /usr/bin/python
$ sudo ln -s /usr/bin/python3.10/usr/bin/python
Note that the path and version of Python 3.x in the second command must be correct.

After the above command is executed, enter the python command in the terminal again, and enter the interactive development environment of Python 3.10.