欢迎阅读今天关于如何在 Ubuntu 22.04/Ubuntu 20.04/Ubuntu 18.04 Linux 系统上安装和配置 Python 3.12 的文章。我们的安装是从源代码执行的,以确保我们构建最新版本的 Python 3.12 编程语言。 Python是一种易于学习和掌握、解释性编程语言,以简单和高度可读而著称。 Python 的第一个版本于 1991 年发布,目前拥有一个由贡献者和开发人员组成的大型活跃社区。
在更新本文时,Python 3.12.x 是 Python 编程语言的最新主要版本。此版本包含许多新功能和优化。一些新功能包括以下内容。
- 支持 Python 代码中的缓冲区协议 (PEP 688)。
- 更灵活的 f 字符串解析,允许许多以前不允许的事情 (PEP 701)。
- 新的调试/分析 API (PEP 669)。
- 更多改进的错误消息。现在,更多可能由拼写错误引起的异常会向用户提出建议。
- 支持具有单独全局解释器锁的隔离子解释器 (PEP 684)。
- 支持 Linux
perf
分析器报告跟踪中的 Python 函数名称。 - 许多大大小小的性能改进(例如 PEP 709 和对 BOLT 二进制优化器的支持),预计整体性能提高 5%。
安装依赖项
在开始安装之前,让我们更新操作系统包索引。
sudo apt update
安装在 Ubuntu 上构建和运行 Python 3.12 所需的所有依赖项。
sudo apt install wget libncurses5-dev build-essential zlib1g-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev pkg-config -y
下载并安装Python 3.12
安装软件包依赖项后,从 Python 官方发布页面下载最新的可用 Python 3.12 gzipped tarball。我们可以使用 wget 命令来做到这一点。
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
下载后解压下载的存档文件。
tar -xf Python-3.12.*.tgz
文件提取后更改为创建的目录。
cd Python-3.12.*/
在安装之前启动 Python 配置。
./configure --enable-optimizations
执行时的示例输出。
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for Python interpreter freezing... ./_bootstrap_python
checking for python3.12... no
checking for python3.12... no
checking for python3.11... no
checking for python3.10... python3.10
checking Python for regen version... Python 3.10.12
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
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
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for egrep... /usr/bin/grep -E
checking for CC compiler name... gcc
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
.....
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/Setup.bootstrap
config.status: creating Modules/Setup.stdlib
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
configure: creating Modules/Setup.local
configure: creating Makefile
现在让我们在 Ubuntu Linux 机器上构建 Python 3.12。 -j 选项用于提供系统上可用于构建过程的内核数量:
make -j $(nproc)
这是我的命令执行输出。
...
if test $? -ne 0 ; then \
echo "generate-posix-vars failed" ; \
rm -f ./pybuilddir.txt ; \
exit 1 ; \
fi
./python -E -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
The necessary bits to build these optional modules were not found:
_dbm _lzma _tkinter
_uuid
To find the necessary bits, look in configure.ac and config.log.
Checked 111 modules (31 built-in, 75 shared, 1 n/a on linux-x86_64, 0 disabled, 4 missing, 0 failed on import)
make[1]: Leaving directory '/root/Python-3.12.2'
构建完成后。然后可以通过运行以下命令来启动 Ansible 安装。
sudo make altinstall
通过使用 altinstall
而不是 install
,Python 二进制路径将为 /usr/bin/python。
执行输出:
.......
Creating directory /usr/local/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.12.1
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--altinstall --upgrade" ;; \
install|*) ensurepip="--altinstall" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpdr48x63t
Processing /tmp/tmpdr48x63t/pip-24.0-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-24.0
完成后,检查版本:
$ python3.12 --version
Python 3.12.2
安装 Python 扩展/模块
在 Python 中,模块提供了 Python 本身不可用的附加功能。模块促进了代码的可重用性以及开发人员之间的一般协作。
Python 包管理器(PIP)用于安装第三方模块。我们可以安装特定的 Pip for Python 版本 3.12。
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
使用以下命令确认 Python Pip 的安装。
$ pip3.12 -V
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
然后可以使用此处显示的命令语法来完成模块的安装。
sudo pip3.12 install module-name
以下是安装 awscli
模块的示例。
$ sudo pip3.12 install awscli
...
Downloading awscli-1.32.49-py3-none-any.whl (4.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 31.3 MB/s eta 0:00:00
Downloading botocore-1.34.49-py3-none-any.whl (12.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.0/12.0 MB 49.0 MB/s eta 0:00:00
Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (724 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 725.0/725.0 kB 19.5 MB/s eta 0:00:00
Downloading s3transfer-0.10.0-py3-none-any.whl (82 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.1/82.1 kB 10.3 MB/s eta 0:00:00
Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Downloading pyasn1-0.5.1-py2.py3-none-any.whl (84 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.9/84.9 kB 12.9 MB/s eta 0:00:00
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 30.9 MB/s eta 0:00:00
Downloading urllib3-2.0.7-py3-none-any.whl (124 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 kB 22.0 MB/s eta 0:00:00
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: urllib3, six, PyYAML, pyasn1, jmespath, docutils, colorama, rsa, python-dateutil, botocore, s3transfer, awscli
Successfully installed PyYAML-6.0.1 awscli-1.32.49 botocore-1.34.49 colorama-0.4.4 docutils-0.16 jmespath-1.0.1 pyasn1-0.5.1 python-dateutil-2.8.2 rsa-4.7.2 s3transfer-0.10.0 six-1.16.0 urllib3-2.0.7
您可以使用以下命令列出所有已安装的模块;
$ pip3.12 list
Package Version
--------------- -------
awscli 1.32.49
botocore 1.34.49
colorama 0.4.4
docutils 0.16
jmespath 1.0.1
pip 24.0
pyasn1 0.5.1
python-dateutil 2.8.2
PyYAML 6.0.1
rsa 4.7.2
s3transfer 0.10.0
setuptools 69.1.1
six 1.16.0
urllib3 2.0.7
wheel 0.42.0
...
结论
Python 是为理解编程概念打下坚实基础的基础,如果你擅长它,肯定会为各种职业机会打开大门。 Python 的安装是解决不同需求和用户需求的项目的起点。
重要链接和资源
- 官方在线文档。
- 您可以通过 GitHub Issues 报告错误。
- 帮助资助 Python 及其社区。