程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

如何在 Ubuntu 20.04|18.04 上安装 Python 3.10

hfteth 2025-02-07 17:38:38 技术文章 23 ℃

#头条创作挑战赛#

Python 是一种广泛使用的、开源的、面向对象的高级编程语言,拥有庞大的社区。它因其简单易学的语法而受到欢迎。Python 可用于构建所有类型的应用程序,从小而简单的脚本到复杂的机器学习算法。

Python 3.10 是 Python 的最新发布版本,但不如 Python 3.9 稳定,但最终候选版本有望很快完成。Python 3.10 具有以下特性:

  1. 能够直接输入提示列表和字典
  2. 更灵活的装饰器
  3. 删除已弃用的兼容性代码
  4. 更强大的 Python 解析器
  5. 新的 HTTP 状态代码
  6. 适当的时区支持
  7. 安全更新等

Python 的一些主要用途是:

  1. 与软件一起使用以创建工作流程。
  2. 在服务器上用于创建 Web 应用程序。
  3. 快速原型制作或生产就绪软件开发。
  4. 处理大数据并执行复杂的数学运算。
  5. 连接到数据库系统,读取和修改文件。

本指南演示如何在 Ubuntu 20.04|18.04 上安装Python 3.10 。我将介绍两种在 Debian 11|10 系统上安装 Python 3.10 的方法。

  • deadsnakes PPA安装 Python 3.10
  • 源代码手动构建 Python 3.10

入门

确保您的系统已更新并安装了所需的软件包。

sudo apt update && sudo apt upgrade -y

选项 1 – 使用 Apt Repo 在 Ubuntu 20.04|18.04 上安装 Python 3.10

使用 APT 在 Ubuntu 20.04|18.04 上安装 Python 3.10 非常简单,对 deadsnakes 自定义 PPA 赞不绝口!这使得在 Ubuntu 上安装 Python 变得容易,并且能够接收持续的更新、错误修复和安全更新。

安装添加自定义 PPA 所需的依赖项。

sudo apt install software-properties-common -y

然后继续并将 deadsnakes PPA 添加到 APT 包管理器源列表中,如下所示。

sudo add-apt-repository ppa:deadsnakes/ppa

Enter 键继续。

..........
To install 3rd-party Python modules, you should use the common Python packaging tools.  For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide:
https://packaging.python.org/installing/

Sources
=======
The package sources are available at:
https://github.com/deadsnakes/

Nightly Builds
==============

For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly
 More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
Press [ENTER] to continue or ctrl-c to cancel adding it

将 deadsnakes 存储库添加到您的 Ubuntu 20.04|18.04 系统后,现在使用下面的单个命令下载 Python 3.10。

sudo apt install python3.10

依赖树:

...................
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3.10-minimal libpython3.10-stdlib python3.10-minimal
Suggested packages:
  python3.10-venv binfmt-support
The following NEW packages will be installed:
  libpython3.10-minimal libpython3.10-stdlib python3.10 python3.10-minimal
0 upgraded, 4 newly installed, 0 to remove and 192 not upgraded.
Need to get 5,023 kB of archives.
After this operation, 19.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

通过检查安装的版本来验证安装。

$ python3.10 --version
3.10.9

选项 2 – 从源代码在 Ubuntu 20.04|18.04 上安装 Python 3.10。

在 Ubuntu 20.04|18.04 系统上安装 Python 3.10 的另一种方法是从源代码构建它。使用这种安装方法,您可以保证获得最新的 Python 版本,尽管您将无法通过APT包管理器接收持续的更新、错误修复和安全更新。

安装所需的依赖项以从源代码构建 Python 3.10。

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

现在继续并从Python 官方发布页面下载最新发布的 Python 版本。或者,复制 Python 3.10 gzipped tarball 的下载链接并使用 Wget 提取它,如下所示。

wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tgz

下载此文件后,继续并解压缩它。

tar -xf Python-3.10.*.tgz

现在导航到提取的目录并运行configure脚本以检查所需的依赖项。–-enable optimization标志通过运行多个测试来优化二进制文件。

cd Python-3.10.*/
./configure --enable-optimizations

输出:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.10... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out

.....................
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile

现在启动 Python 3.10 构建过程,如下所示。

make -j $(nproc)

请记住,-j 参数应与您系统上的内核数量相匹配,以使进程更快。使用命令检查系统上的内核数nproc。对于这个例子,我的系统上有 2 个内核。

示例输出:

....................
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  pwd                   time               

running build_scripts
copying and adjusting /home/ubuntu/Python-3.10.3/Tools/scripts/pydoc3 -> build/scripts-3.10
copying and adjusting /home/ubuntu/Python-3.10.3/Tools/scripts/idle3 -> build/scripts-3.10
copying and adjusting /home/ubuntu/Python-3.10.3/Tools/scripts/2to3 -> build/scripts-3.10
changing mode of build/scripts-3.10/pydoc3 from 644 to 755
changing mode of build/scripts-3.10/idle3 from 644 to 755
changing mode of build/scripts-3.10/2to3 from 644 to 755
renaming build/scripts-3.10/pydoc3 to build/scripts-3.10/pydoc3.10
renaming build/scripts-3.10/idle3 to build/scripts-3.10/idle3.10
renaming build/scripts-3.10/2to3 to build/scripts-3.10/2to3-3.10
make[1]: Leaving directory '/home/ubuntu/Python-3.10.0'

构建过程完成后,现在发出下面的 make 命令以在 Ubuntu 20.04|18.04 上安装 Python 3.10。

sudo make altinstall

这里 altinstall使用而不是install为了在/usr/bin/python中保留默认的 Python 二进制路径。

示例输出:

Looking in links: /tmp/tmpwbk6rtev
Processing /tmp/tmpwbk6rtev/setuptools-57.4.0-py3-none-any.whl
Processing /tmp/tmpwbk6rtev/pip-21.2.3-py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: Value for scheme.headers does not match. Please report this to 
  distutils: /usr/local/include/python3.10/setuptools
  sysconfig: /home/ubuntu/Python-3.10.3/Include/setuptools
  WARNING: Value for scheme.headers does not match. Please report this to 
  distutils: /usr/local/include/python3.10/pip
  sysconfig: /home/ubuntu/Python-3.10.3/Include/pip
Successfully installed pip-21.2.3 setuptools-57.4.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

验证您的安装。

$ python3.10 --version
Python 3.10.9

在 Ubuntu 20.04|18.04 上安装 Python 模块|扩展

模块和扩展在 Python 中很有用,因为它们为它添加了功能。可以使用Python 包管理器(PIP)在 Ubuntu 20.04|18.04 上安装模块。您需要在系统上安装 PIP,如下所示

sudo apt install python3-pip

然后使用以下语法安装所选的 Python 模块。

sudo pip install module-name

在本指南中,我将演示如何安装 Python 模块beautifulsoup4

sudo pip install beautifulsoup4

输出:

Collecting beautifulsoup4
  Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 259 kB/s 
Collecting soupsieve>1.2
  Downloading soupsieve-2.2.1-py3-none-any.whl (33 kB)
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.10.0 soupsieve-2.2.1

您可以使用以下命令列出本地安装的 Python 包:

$ pip list
Package                Version             
---------------------- --------------------
attrs                  19.3.0              
Automat                0.8.0               
beautifulsoup4         4.10.0              
blinker                1.4                 
certifi                2019.11.28          
chardet                3.0.4               
Click                  7.0                 
cloud-init             20.2                
colorama               0.4.3               
command-not-found      0.3                 
configobj              5.0.6               
constantly             15.1.0              
cryptography           2.8                 
dbus-python            1.2.16              
distro                 1.4.0               
distro-info            0.23ubuntu1         
entrypoints            0.3                 
httplib2               0.14.0              
hyperlink              19.0.0     
..............

这就对了!我希望你喜欢这篇关于如何在 Ubuntu 20.04|18.04 上安装 Python 3.10 的指南。您现在可以使用 Python 构建 Web 应用程序、软件开发、创建工作流等

最近发表
标签列表