如何在 CentOS、Ubuntu 和 Windows 上安装 Python 3?

Python 编程语言简介及在不同操作系统上的安装指南

Python 是一种开源、功能强大、易于理解且跨平台的编程语言。它在数据分析和机器学习领域中尤为流行。Python 提供了丰富的内置和可移植库,这些库可以方便地扩展 Python 项目的功能。此外,它还全面支持面向对象和函数式编程的实践,适用于各类软件工程项目。

目前,Python 主要有 Python 2.x 和 Python 3.x 两个版本。两者之间存在一些不兼容之处,因此在选择版本时,应根据当前的环境和实际需求进行谨慎考虑。由于 Python 在操作系统库和软件中的广泛应用,大多数 Linux 发行版都预装了 Python 的某个版本。

本文将重点介绍如何在 CentOS、Ubuntu 和 Windows 等不同平台上安装 Python 3。虽然 Python 3 可以与 Python 2 共存,但用户在设置系统默认的 Python 解释器时需要格外小心,以避免潜在的冲突。

在 CentOS 6.x/7.x 上安装 Python 3

对于 CentOS 6.x/7.x 或基于 RHEL 的发行版,您可以使用 yum 包管理器来安装 Python 3。请使用以下命令:

$ sudo yum install python3

在安装过程中,系统会提示您确认,请输入“y”:

Is this ok [y/d/N]: y

以下是 CentOS 7 上安装 Python 3 的日志示例:

$ sudo yum install python3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-18.el7 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.6.8-18.el7 for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: python3-pip for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python3-3.6.8-18.el7.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.6.8-18.el7 will be installed
--> Processing Dependency: libtirpc.so.1()(64bit) for package: python3-libs-3.6.8-18.el7.x86_64
---> Package python3-pip.noarch 0:9.0.3-8.el7 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed
--> Running transaction check
---> Package libtirpc.x86_64 0:0.2.4-0.16.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================================================
Installing:
python3 x86_64 3.6.8-18.el7 updates 70 k
Installing for dependencies:
libtirpc x86_64 0.2.4-0.16.el7 base 89 k
python3-libs x86_64 3.6.8-18.el7 updates 6.9 M
python3-pip noarch 9.0.3-8.el7 base 1.6 M
python3-setuptools noarch 39.2.0-10.el7 base 629 k

Transaction Summary
==========================================================================================================================================================================
Install 1 Package (+4 Dependent packages)

Total download size: 9.3 M
Installed size: 48 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): python3-3.6.8-18.el7.x86_64.rpm | 70 kB 00:00:00
(2/5): libtirpc-0.2.4-0.16.el7.x86_64.rpm | 89 kB 00:00:00
(3/5): python3-setuptools-39.2.0-10.el7.noarch.rpm | 629 kB 00:00:01
(4/5): python3-pip-9.0.3-8.el7.noarch.rpm | 1.6 MB 00:00:06
(5/5): python3-libs-3.6.8-18.el7.x86_64.rpm | 6.9 MB 00:00:15
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 613 kB/s | 9.3 MB 00:00:15
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libtirpc-0.2.4-0.16.el7.x86_64 1/5
Installing : python3-setuptools-39.2.0-10.el7.noarch 2/5
Installing : python3-pip-9.0.3-8.el7.noarch 3/5
Installing : python3-3.6.8-18.el7.x86_64 4/5
Installing : python3-libs-3.6.8-18.el7.x86_64 5/5
Verifying : libtirpc-0.2.4-0.16.el7.x86_64 1/5
Verifying : python3-setuptools-39.2.0-10.el7.noarch 2/5
Verifying : python3-libs-3.6.8-18.el7.x86_64 3/5
Verifying : python3-3.6.8-18.el7.x86_64 4/5
Verifying : python3-pip-9.0.3-8.el7.noarch 5/5

Installed:
python3.x86_64 0:3.6.8-18.el7

Dependency Installed:
libtirpc.x86_64 0:0.2.4-0.16.el7 python3-libs.x86_64 0:3.6.8-18.el7 python3-pip.noarch 0:9.0.3-8.el7 python3-setuptools.noarch 0:39.2.0-10.el7

Complete!
$

在 CentOS 8.x 上安装 Python 3

在 CentOS 8.x 上,可以使用 dnf 包管理器来安装 Python 3。 执行以下命令开始安装:

$ sudo dnf install python3

系统会提示您确认,请输入“y”:

Is this ok [y/N]: y

以下是 CentOS 8 上安装 Python 3 的日志片段:

$ sudo dnf install python3
Last metadata expiration check: 6:25:17 ago on Friday 11 December 2020 12:44:46 PM IST.
Package python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 is already installed.
Dependencies resolved.
==========================================================================================================================================================================
Package Architecture Version Repository Size
==========================================================================================================================================================================
Upgrading:
python36 x86_64 3.6.8-2.module_el8.3.0+562+e162826a AppStream 19 k

Transaction Summary
==========================================================================================================================================================================
Upgrade 1 Package

Total download size: 19 k
Is this ok [y/N]: y
Downloading Packages:
python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64.rpm 5.6 kB/s | 19 kB 00:03
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 4.6 kB/s | 19 kB 00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Upgrading : python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 1/2
Running scriptlet: python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 1/2
Cleanup : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/2
Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/2
Verifying : python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 1/2
Verifying : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/2
Installed products updated.

Upgraded:
python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64

Complete!
$

在 Ubuntu 上安装 Python 3

对于运行 Ubuntu/Debian 的操作系统,我们可以使用 apt 或 apt-get 来安装 Python 3。

较新的 Ubuntu 系统通常已经预装了 Python 3。如果出于某种原因它被删除,您需要明确地安装它,可以使用以下命令:

$ sudo apt install python3

在 Linux 系统上验证 Python 3 安装

您可以通过 Linux 终端轻松检查 Python 3 的安装情况。要查看已安装的版本,请使用以下命令:

$ python3 --version
Python 3.8.6
$

在 Windows 上安装 Python 3

在 Windows 操作系统上,您可以使用来自 Python 官方网站的安装程序来轻松安装 Python 3。官方提供的默认安装程序 下载页面 通常足以满足大多数用途,除非您需要选择特定的 Python 版本或架构。

在撰写本文时,最新的可用版本是 3.9.1。

对于 64 位架构,您下载的 Python 安装程序文件名称将类似于 python-3.xx-amd64.exe。

  • 双击下载的 .exe 文件启动安装程序。
  • 安装程序默认选项通常足够,但您可以选择自定义安装选项来修改安装位置和组件。
  • 您还可以选择将 Python 3 安装给所有用户或仅限于特定的 Windows 用户账户。
  • 安装过程中,可以选择将 Python 3 添加到 Windows 的 PATH 环境变量中,这样您就可以从任何目录的 Windows 命令提示符中使用 Python 3。
  • 确认所有设置后,单击“立即安装”以继续使用默认选项安装 Python 3。

  • 安装对话框会显示安装进度。请耐心等待其完成。

  • 安装完成后,您将在 Windows 的“开始”菜单中看到 Python 3.x 的图标,点击可以打开 Python 3 的交互式命令行界面(CLI)。

  • 交互式 Python 3 CLI 允许您以交互方式编写和执行 Python 命令。例如,您可以使用 `print` 命令在 Python 中编写一个简单的“Hello World”程序。由于这是一个交互式 CLI,命令会被立即解释并打印输出。

为了更好地练习代码,建议您安装一个 Python IDE(集成开发环境)。

拓展阅读

您可以通过查阅详细的 官方文档 或者参加在线课程来深入学习 Python。

接下来,您还可以学习如何安装 PIP 来管理 Python 包。

如果您喜欢本文,请分享给更多的人。