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 版

第 2 章 Python 如何运行程序 Python 如何运行程序

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章和下一章将快速了解程序的执行--如何启动代码,以及 Python 如何运行代码。在本章中,我们将研究 Python 解释器是如何执行程序的。然后,第 3 章将向您展示如何启动和运行您自己的程序。

启动细节 本身就与特定平台有关,这两章中的某些内容可能不适用于您的工作平台,因此高级读者可以跳过与其预期用途无关的部分。同样,过去曾使用过类似工具并希望快速掌握语言要领的读者可能希望将其中一些章节归档,作为 "供今后参考"。对于我们其他人来说,在学习如何编写代码之前,让我们先简单了解一下 Python 运行代码的方式。

Python 解释器介绍

到目前为止,我主要是在谈论作为编程语言的 Python 。但就目前的实现而言,它也是一个称为解释器的软件包。解释器是一种执行其他程序的程序。当您编写 Python 程序时,Python 解释器会读取您的程序并执行其中包含的指令。实际上,解释器是你的代码和计算机硬件之间的一层软件逻辑。

当 Python 软件包安装到你的计算机上时,它会生成一些组件--主要是一个解释器和一个支持库。 根据你使用它的方式,Python 解释器可能是一个可执行程序的形式,也可能是一组链接到另一个程序的库的形式。 根据你运行的 Python 版本,解释器本身可能以 C 程序、一组 Java 类或其他形式实现。 无论以何种形式,你编写的 Python 代码必须始终由这个解释器运行。要做到这一点,你必须在计算机上安装一个 Python 解释器。

Python 的安装细节因平台而异,附录 A 对其进行了更深入的介绍。简而言之

  • Windows 用户获取并运行一个自安装可执行文件,将 Python 安装到他们的机器上。只需双击并在所有提示下说 "是 "或 "下一步 "即可。

  • Linux 和 Mac OS X 用户可能已经在电脑上预装了可用的 Python--这是目前这些平台上的标准组件。

  • 一些 Linux 和 Mac OS X 用户(以及大多数 Unix 用户),从 Python 的完整源代码发布包中编译 Python。

  • Linux 用户还可以找到 RPM 文件,Mac OS X 用户可以找到各种 Mac 专用的安装包。

  • 其他平台也有与之相关的安装技术。例如,Python 可在手机、平板电脑、游戏机和 iPod 上使用,但安装细节差别很大。

Python 本身可从其主网站http://www.python.org 的下载页面获取。您也可以通过其他各种分发渠道找到它。请记住,在安装 Python 之前,应始终检查它是否已经存在。如果您使用的是 Windows 7 及更早版本,通常会在 "开始 "菜单中找到 Python,如图 2-1 所示;我们将在下一章讨论这里显示的菜单选项。在 Unix 和 Linux 上,Python 可能位于/usr目录树中。

由于安装细节与特定平台密切相关,我们将在此暂缓介绍其余部分。有关安装过程的详细信息,请参阅附录 A。在本章和下一章中,我将假设您已经准备好了 Pythongo。

计划执行

编写和运行 Python 脚本意味着什么,这取决于您是以程序员的身份,还是以 Python 解释器的身份来看待这些任务。这两种观点都为 Python 编程提供了重要的视角。

图 2-1. 安装在 Windows 7 及更早版本上时,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