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 版

第 3 章 如何运行程序 如何运行程序

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

好了,是时候开始运行一些代码了。现在,您已经掌握了程序执行模型,终于可以开始真正的 Python 编程了。在这一点上,我假设您的计算机上安装了 Python;如果没有,请参阅上一章的开头和附录 A,了解各种平台上的安装和配置提示。我们的目标是学习如何运行 Python 程序代码。

有多种方法可以让 Python 执行您键入的代码。 本章将讨论目前常用的所有程序启动技术。在此过程中,您将学习如何交互式地键入代码,以及如何将代码保存到文件中,以便以各种方式运行:系统命令行、图标点击、模块导入、exec 调用、IDLE GUI 中的菜单选项等。

至于前一章,如果您已有编程经验,并急于开始深入研究 Python 本身,您可能想略读本章,然后继续阅读第 4 章。但是,不要跳过本章对前言和约定的早期介绍、对调试技术的概述,或者对模块导入的初步了解--模块导入是理解 Python 程序结构的关键,我们将在后面的章节中再讨论这个问题。我还鼓励您查看有关 IDLE 和其他集成开发环境的章节,这样当您开始开发更复杂的 Python 程序时,您就会知道有哪些工具可用。

互动提示

本节让我们开始学习交互式编码基础知识。因为这是我们第一次接触运行代码,所以这里还包括一些预备知识,如设置工作目录和系统路径,因此如果你是编程新手,请务必先阅读这一部分。本节还解释了本书中使用的一些约定,因此大多数读者可能至少应该在这里快速浏览一下。

开始互动会话

运行 Python 程序的最简单的方法也许是在 Python 的交互式命令行(有时也称为交互式提示符)上键入程序。启动命令行的方法有很多种:在集成开发环境中、从系统控制台等等。 假设解释器作为可执行程序安装在系统中,那么启动交互式解释器会话的最平台中立的方法通常是在操作系统的提示符下键入 python不带任何参数。例如

% python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit ...
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

在 输入 "python",系统 shell 提示符就会像这样开始一个 Python 交互会话;本列表开头的"%"字符代表本书中的通用系统提示符,它不是您自己输入的内容。在 Windows 上,按Ctrl-Z可以退出会话;在 Unix 上,按Ctrl-D可以退出会话。

系统 shell 提示的概念是通用的,但具体如何访问则因平台而异:

  • Windows 系统中,。 python 在 DOS 控制台窗口中键入 - 该程序名为cmd.exe ,通常称为 Command Prompt。有关启动该程序的更多详情,请参阅本章边栏"Windows 上的 Command Prompt 在哪里?

  • Mac OS X 上,,双击 Applications→Utilities→Terminal 即可启动 Python 交互式解释器,然后在打开的窗口中输入 python然后在打开的窗口中输入

  • Linux(和其他 Unix 系统)上,,在 shell 或终端窗口(例如,在运行ksh或 csh 等 shell 的xterm或控制台)中键入此命令。

  • 其他系统可能会使用类似或特定平台的设备。例如,在手持设备上,您可以点击主窗口或应用程序窗口中的 ...

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