引言

ftp,select

好,是时候开始运行一些代码了。既然你已经掌握了程序执行模型,你终于准备好开始一些真正的 Python 编程了。在此时,我将假设你已经在电脑上安装好了 Python;如果没有,参见前一章的开始部分和附录 A 来获取各平台上的安装和配置提示。我们这里的目标是学习如何运行 Python 程序代码。 有多种方式让 Python 去执行你键入的代码。本章讨论了日常使用的所有程序启动技术。在此过程中,你将学习如何交互式地键入代码,以及如何将代码保存到文件中,尽可能多的以各种方式运行代码:使用系统命令行,点击图标,模块导入,<font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">exec</font> 调用,IDLE GUI 中的目录选项等等。 和前一章一样,如果你之前有编程经验且急着开始深入研究 Python 本身,你可以略读本章并前进到第 4 章。但不要跳过本章开头对基本概念和约定的讨论,它的调试技术总览,或对模块导入的第一次介绍 —— 理解 Python 程序架构的重要主题,直到后面的一个大部分(注:本书共分为几个大部分),我们将不会再谈到它。我还鼓励你阅读关于 IDLE 和其他 IDEs 的部分,这样你将在开始开发更复杂的 Python 程序时,知道有哪些工具可用。

交互的提示符

这一部分让我们从交互式编码基础开始。因为是第一次了解运行代码,我们还在这里讲到一些基本概念,如设置工作目录和系统路径,所以如果你对编程还比较陌生,确保先读这部分。这部分还解释了贯穿本书的一些约定,所以大部分读者都应该至少快速的了解一下这里。

交互的提示符——开启交互会话

可能运行 Python 程序的最简单的方法就是在 Python 的交互式命令行(有时叫做交互式提示符)键入它们。有许多方式启动这个命令行:在 IDE 中,从系统控制台等等。假设解释器被作为一个可执行程序安装在你的系统中,最通用的与平台无关的启动一个交互式解释器会话的方法通常是:在操作系统的提示符那里键入 python(不带任何参数)。比如:

3.1 提示 - 图1

在像上面这样的系统 shell 提示符那里键入单词 “python” 会启动一个交互式的 Python 会话;在这个列表开始的 “%” 符号代表本书中的通用系统提示符 —— 它不是你自己键入的输入。在 Windows 上, Ctrl-Z 退出这个会话;在 Unix 上,尝试 Ctrl-D. 系统 shell 提示符的概念是通用的,但在各种平台上如何去访问它方法各异:
  • Windows 上,可以在一个 Dos 控制台窗口(名为 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">cmd.exe</font> 和通常被称为命令提示符 的程序)键入 python 。关于启动这个程序的更多细节,参见本章的侧边栏 ——Windows 上的命令提示符在哪?
  • Mac OS X 上,可以通过在 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">应用程序->实用工具->终端</font> 图标上双击来开启一个 Python 交互式解释器,然后再弹出的窗口中键入 python
  • Linux(和其他 Unixes) 上,可以在 shell 或终端窗口键入这个命令(比如,在一个 xterm 或运行着如 ksh 或 csh 等 shell 的控制台)
  • 其他系统可能使用类似的或平台专用的设备。比如,在手持设备上,你可以在 home 或者应用窗口单击 Python 图标来开启一个交互式会话。
在大多数平台上,可以用不需要键入命令的其他方式来开启交互式提示符,但他们在各平台上的差异就更大了:
  • Windows7 和之前的系统,除了在 shell 窗口中键入 python,还可以通过启动 IDLE GUI(随后讨论)或开始按钮目录中选择 “Python(命令行)” 目录选项,如第 2 章图 2-1 展示的那样。这两种方式都创建了一个 Python 交互式提示符,和用 “python” 命令获得的功能相同。
  • Windows8 上,没有开始按钮(至少在我写本书时如此),但有其他方式获得前面列表项中讨论的工具,包括 tiles(注:win8 中的 “图块” 功能),搜索,文件管理器,和开始屏幕上的 “所有 apps” 界面。参见附录 A 获取关于这个平台的更多建议。
  • 其他平台有类似的无需键入命令的开启 Python 交互式会话的方法,但它们都太具体以至于不能在这里深入讨论下去;参见你的系统文档来获取细节。
当你看到 >>> 提示符时,就处于一个交互式 Python 解释器会话中 —— 你可以在这里键入任何 Python 语句或表达式,然后立即运行。我们过一会儿就会试一试,但首先需要将一些启动细节理清楚来确保所有读者准备就绪。 本文章首发在 LearnKu.com 网站上。

上一篇下一篇

Markdown 文本

纠错改进


交互的提示符——开启交互会话——Windows上的命令提示符在哪?

So how do you start the command-line interface on Windows? Some Windows readers already know, but Unix developers and beginners may not; it’s not as prominent as terminal or console windows on Unix systems. Here are some pointers on finding your Command Prompt, which vary slightly per Windows version. On Windows 7 and earlier, this is usually found in the Accessories section of the Start→All Programs menu, or you can run it by typing cmd in the Start→Run… dialog box or the Start menu’s search entry field. You can drag out a desktop shortcut to get to it quicker if desired. On Windows 8, you can access Command Prompt in the menu opened by right-clicking on the preview in the screen’s lower-left corner; in the Windows System section of the “All apps” display reached by right- clicking your Start screen; or by typing cmd or command prompt in the input field of the Search charm pulled down from the screen’s upper-right corner. There are probably additional routes, and touch screens offer similar access. And if you want to forget all that, pin it to your desktop taskbar for easy access next time around. These procedures are prone to vary over time, and possibly even per computer and user. I’m trying to avoid making this a book on Windows, though, so I’ll cut this topic short here. When in doubt, try the system Help interface (whose usage may differ as much as the tools it provides help for!). A note to any Unix users reading this sidebar who may be starting to feel like a fish out of water: you may also be interested in the Cygwin system, which brings a full Unix command prompt to Windows. See Appendix A for more pointers. 注:由于这里的文字早已过时(win7 和 win8),所以不再翻译。

交互的提示符——系统路径 path

当我们在上一部分键入 python 来开启一个交互式会话时,依赖于这么一个事实:系统根据它的程序搜索路径来为我们定位 Python 程序。取决于你的 Python 版本和平台,如果你还没有设置系统的 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">PATH</font> 环境变量来包括 Python 的安装目录,你就可能需要使用你机器上的 Python 可执行文件的全路径来替换单词 “python”。在 Unix,Linux 和类似的系统上,像 /usr/local/bin/python 或 /usr/bin/python3 通常就足够了。在 Windows 上,尝试输入:C:\Python33\python(对版本 3.3):

3.1 提示 - 图2

另外,你可以在键入 python 前运行 “cd”(改变目录)命令来进入 Python 的安装目录 —— 比如,在 Windows 上尝试 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">cd c:\python33</font> 3.1 提示 - 图3 但你最终还是很可能要设置 PATH,这样,只要输入 “python” 就够了。如果你不知道 PATH 是什么或如何设置它,参见附录 A—— 它涵盖了像这样用法在各平台上各异的环境变量,还有我们将不会在本书中经常使用的 Python 命令行参数。给 Windows 用户的快速提示:控制面板 -> 系统 -> 高级设置。如下面一节解释的,如果你在使用 Python3.3 和之后的版本,在 Windows 上这个设置是自动的。

交互的提示符——在3.3中的新的Windows选项——PATH,启动器 e

The foregoing section and much of this chapter at large
describe the generic state of play for all 2.X and 3.X Pythons prior to version 3.3. Starting with Python 3.3, the Windows installer has an option to automatically add Python 3.3’s directory to your system PATH, if enabled in the installer’s windows. If you use this option, you won’t need to type a directory path or issue a “cd” to run python commands as in the prior section. Be sure to select this option during the install if you want it, as it’s currently disabled by default. More dramatically, Python 3.3 for Windows ships with and automatically installs the new Windows launcher—a system that comes with new executable programs, py with a console and pyw without, that are placed in directories on your system path, and so may be run out of the box without any PATH configurations, change-directory commands, or directory path prefixes:
  1. c:\code> py
  2. Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600
  3. 64 bit ...
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>
  6. c:\code> py 2
  7. Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit
  8. (AMD64)] ...
  9. Type "help", "copyright", "credits" or "license" for more information.
  10. >>>
  11. c:\code> py 3.1
  12. Python 3.1.4 (default, Jun 12 2011, 14:16:16) [MSC v.1500 64 bit
  13. (AMD64)] ...
  14. Type "help", "copyright", "credits" or "license" for more information.
  15. >>>
As shown in the last two commands here, these executables also accept Python version numbers on the command line (and in Unix-style #! lines at the top of scripts, as discussed later), and are associated to open Python files when clicked just like the original python executable—which is still available and works as before, but is somewhat superseded by the launcher’s new programs. The launcher is a standard part of Python 3.3, and is available standalone for use with other versions. We’ll see more on this new launcher in this and later chapters, including a brief look at its #! line support here. However, because it is of interest only to Windows users, and even for this group is present only in 3.3 or where installed separately, I’ve collected almost all of the details about the launcher in Appendix B. If you’ll be working on Windows under Python 3.3 or later, I suggest taking a brief detour to that appendix now, as it provides an alternative, and in some ways better, way to run Python command lines and scripts. At a base level, launcher users can type py instead of python in most of the system commands shown in this book, and may avoid some configuration steps. Especially on computers with multiple Python versions, though, the new launcher gives you more explicit control over which Python runs your code. 注:这一节全是在讲 windows 环境,暂时不翻译。

交互的提示符——在哪里运行:代码目录

既然我已经开始向你展示如何运行代码,关于在哪里运行代码我想提前说几句。为简单起见,本章和本书中我大致都会从一个工作目录(又名文件夹)来运行代码,我已经在我的 Windows 电脑上创建了它,名为 C:\code —— 一个在我的硬盘主驱动器顶部的子目录。这是我将开启大多数交互式会话和保存并运行大多数脚本文件的地方。这也意味着例子中创建的文件将大部分显示在此目录中。 如果你跟着例子一起练习,你很可能应该在我们开始前做一些类似的事情。如果你需要在电脑上设置工作目录的帮助,这里是一些建议:
  • 在 Windows 上,你可以在文件管理器或命令提示符窗口创建工作代码目录。在文件管理器,寻找 “新建文件夹”,参见 “文件” 目录,或尝试右键。在命令提示符中,键入并运行 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">mkdir</font> 命令,通常在你 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">cd</font> 到你所想要的父目录中(比如,<font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">cd c:\</font> 然后 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">mkdir code</font>)。你的工作目录可以放在任何你喜欢的地方,命名为任何你希望的名字,且不需要为 C:\code(我选择这个名字只是因为它在提示符中简短)。但只在一个目录(注:而不是很多乱七八糟的目录)中运行代码将帮助你跟踪工作并简化任务。对于更多的 Windows 提示,参考本章关于命令提示符的侧边栏和附录 A。
  • 基于 Unix 的系统(包括 Mac OS X 和 Linux)上,工作目录可以在 /usr/home 然后在一个 shell 窗口或平台特定的文件管理器 GUI 中,通过 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">mkdir</font> 命令被创建,但概念相同。用于 Windows 的 Cygwin 类 Unix 系统也是类似的,然而目录名称可能不同(/home /cygdrive/c 都是候选)。
你还可以将代码存储在 Python 的安装目录(比如在 Windows 上的 C:\Python33)来在设置 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">PATH</font> 前简化一些命令行,但你很可能不应该这么做 —— 这个目录是 Python 本身的,你的文件将可能在移动或卸载的过程中丢失)。 一旦你创建了工作目录,就总是在那里开始跟着本书中的例子学习吧。本书中的那些显示了我正在运行代码的目录的提示符反映了我 Windows 笔记本上的工作目录。当你看到 <font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">C:\code</font><font style="color:rgb(133, 128, 128);background-color:rgb(249, 250, 250);">%</font> 时,想象为你自己的目录位置和名称。