Linux(Ubuntu)系统安装Python

  • 内容
  • 评论
  • 相关

Linux 系统是为编程而生的,因此绝大多数的 Linux 发行版(Ubuntu、CentOS 等)都默认自带了 Python。有的 Linux 发行版甚至还会自带两个版本的 Python,例如最新版的 Ubuntu 会自带 Python 2.x 和 Python 3.x。

打开 Linux 发行版内置的终端(Terminal),输入python命令就可以检测是否安装了 Python,以及安装了哪个版本,如下所示:

[c.biancheng.net@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.
>>>

可以看到,python命令能够正常运行,并输出了 Python 的版本信息,这表明当前的 Linux 发行版已经自带了 Python 2.7.5。

另外,执行结果最后出现了 Python 命令提示符>>>,这意味着我们进入了 Python 交互式编程环境,可以在这里直接输入代码并查看运行结果,如下所示:

[c.biancheng.net@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("C的网址是:http://c.biancheng.net")
C的网址是:http://c.biancheng.net
>>> a=100
>>> b=4
>>> a*b
400
>>> exit()
[c.biancheng.net@localhost ~]$ 

exit() 用来退出 Python 编程环境,回到 Linux 命令行。

大部分的 Linux 发行版会自带 Python 2.x,但是不一定自带 Python 3.x,要想检测当前 Linux 发行版是否安装了 Python 3.x,可以在终端(Terminal)输入python3命令,如下所示:

[c.biancheng.net@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.
>>>

如果python3命令运行成功,并出现 Python 提示符>>>,则表明当前 Linux 发行版已经安装了 Python 3 开发环境,只需执行python3命令就可以启动 Python 3 开发环境。

如果当前 Linux 发行版没有安装 Python 3,或者你觉得现有的 Python 3 版本不够新,那么就需要更新 Python 版本。本节我们以 Ubuntu 为例来进行演示。

更新 Python 版本

在 Ubuntu 终端执行以下两条命令即可更新 Python 版本:

$sudo apt-get update
$sudo apt-get install python3.8

对命令的说明:

  • 第一条命令用来指定更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d 所列出的源地址,这样能够保证获得最新的安装包。
  • 第二条命令用来指定安装 Python 3.8,这是目前最新的 Python 版本。

等待以上两条命令执行完成,再次在终端输入python3命令,就可以看到 Python 交互式编程环境已经更新到 Python 3.8。

重新安装 Python

以上更新方法仅在 Ubuntu 已经安装 Python 的情况下才有效,如果你的 Ubuntu 中没有 Python 环境,或者你想重新安装,那么就得到官网下载源代码,然后自己编译。

1) 下载源代码

Python 官方下载地址:https://www.python.org/downloads/

打开链接,可以看到各个版本的 Python:

Python 下载页面截图
图 1 Python 下载页面截图

本文标题:Linux(Ubuntu)系统安装Python

本文地址:https://www.hosteonscn.com/5778.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注