Skip to Content
《学习 Python》第 5 版
book

《学习 Python》第 5 版

by Mark Lutz
May 2025
Intermediate to advanced
1648 pages
23h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 Python》第 5 版

附录 B. Python 3.3 Windows 启动器

本附录介绍了 Python 的新 Windows 启动器,该启动器随 Python 3.3 自动安装,也可在 Web 上单独获得,用于旧版本。这个新的启动器提供了一个额外的代码层,用于选择和启动已安装的 Python。 虽然它也有一些缺陷,但当多 Python 同时存在于同一台计算机上时,启动器为程序的执行提供了一些急需的一致性。

我为在 Windows 上使用 Python 的程序员编写了这一页。尽管从本质上讲它是特定于平台的,但它的目标读者既包括 Python 初学者(他们中的大多数人都是从这个平台入门的),也包括编写代码以便在 Windows 和 Unix 之间可移植地工作的 Python 开发人员。正如我们将看到的,新的启动器彻底改变了 Windows 上的规则,足以影响每个在 Windows 上使用 Python 的,或者将来可能使用的人。

Unix 的遗产

要完全理解启动器的协议,我们必须先上一堂简短的历史课。很早以前,Unix 开发人员就设计了一种协议,用于指定运行脚本代码的程序。在 Unix 系统(包括 Linux 和 Mac OS X)上,如果脚本文本文件的第一行以两个字符序列开头,那么该行就是特殊的:#!在 Unix 系统(包括 Linux 和 Mac OS X)上,脚本文本文件的第一行如果以两个字符序列开头,就很特别,有时也称为 "shebang"(我保证以后不再重复这个愚蠢的短语)。

第 3 章简要介绍了这一主题,下面我们再来看看。在 Unix 脚本中,这样的行指定一个程序来运行脚本的其余内容,方法是在#!后编码--使用所需的程序本身的目录路径,或者调用env Unix 工具,该工具根据PATH 设置查找目标,这是一个可定制的系统环境变量,用于列出可执行文件的搜索目录:

#!/usr/local/bin/python
...script's code              # Run under this specific program

#!/usr/bin/env python
...script's code              # Run under "python" found on PATH

通过使这样的脚本可执行(例如通过chmod +x script.py ),只需在命令行中给出文件名就可以运行该脚本;顶部的#! 行将引导 Unix shell 进入一个程序,该程序将运行文件的其余代码。根据平台的安装结构,这些#! 行命名的python 可能是一个真正的可执行文件,也可能是一个符号链接,指向位于其他地方的特定版本的可执行文件。这些行也可能明确命名一个更具体的可执行文件,如python3 。无论哪种方式,通过更改#! 行、符号链接或PATH 设置,Unix 开发人员都可以将脚本路由到适当的已安装 Python。

当然,这些都不适用于 Windows 本身,在 Windows 中,#! 行没有固有的意义。Python 本身历来忽略 Windows 上的注释行("#"在语言中是注释的开头)。不过,在 Python 2.X 和 3.X 经常在同一台机器上共存的情况下,按文件选择 Python 可执行文件的想法还是很有吸引力的。鉴于许多程序员编写#! 行都是为了能够移植到 Unix 上,这个想法似乎已经成熟,可以效仿。

Windows 的遗产

另一侧的安装模式则截然不同。在过去(嗯,在 3.3 之前的每一个 Python 中),Windows 安装程序都会更新全局 Windows 注册表,这样,安装在您计算机上的最新 Python ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656963