Learning Python-the third chapter

How You Run Programs
OK, it’s time to start running some code. Now that you have a handle on the program execution(执行) model, you’re finally ready to start some real Python programming. At this point(从这一点上), I’ll assume(假定为) that you have Python installed on your computer; if you don’t, see the start of the prior(先前的) chapter(章节) and Appendix A for installation and configuration(结构) hints(提示) on various platforms. Our goal here is to learn how to run Python program code.
There are multiple(多样的) ways to tell Python to execute the code you type. This chapter discusses all the program launching techniques in common use today. Along the way, you’ll learn how to both type code interactively, and how to save it in files to be run as often as you like in a variety of ways: with system command lines, icon clicks(图标点击), module imports(进口模块), exec calls(执行调用), menu options(菜单选项) in the IDLE GUI, and more.
As for the previous chapter, if you have prior(先前的) programming experience and are anxious to start digging into Python itself, you may want to skim(浏览) this chapter and move on to Chapter 4. But don’t skip this chapter’s early coverage of preliminaries and conventions(协定), its overview (概要)of debugging techniques, or its first look at module imports—a topic essential(本质的) to understanding Python’s program architecture(格局), which we won’t revisit until a later part. I also encourage you to see the sections(部分) on IDLE and other IDEs, so you’ll know what tools are available when you start developing more sophisticated(久经世故的 ) Python programs.
The Interactive Prompt(交互提示符)
This section gets us started with interactive coding basics. Because it’s our first look at running code, we also cover some preliminaries here, such as setting up a working directory and the system path, so be sure to read this section first if you’re relatively new to programming. This section also explains some conventions used throughout the book, so most readers should probably take at least a quick look here.
Starting an Interactive Session
Perhaps the simplest way to run Python programs is to type them at Python’s interactive command line(交互式命令行), sometimes called the interactive prompt. There are a variety of ways to start this command line: in an IDE, from a system console(控制台), and so on. Assuming the interpreter is installed as an executable(执行的) program on your system, the most platform-neutral (平台无关的)way to start an interactive interpreter session is usually just to type python at your operating(操作的) system’s prompt(提示)without any arguments(参数). For example:

% 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

Typing the word “python” at your system shell prompt like this begins an interactive Python session(会话); the “%” character at the start of this listing stands for a generic(通用的) system prompt in this book—it’s not input that you type yourself. On Windows, a Ctrl-Z gets you out of this session; on Unix, try Ctrl-D instead.
The notion of a system shell prompt is generic, but exactly how you access it varies by platform:
• On Windows, you can type python in a DOS console window—a program named cmd.exe and usually known as Command Prompt. For more details(详细的) on starting this program, see this chapter’s sidebar “Where Is Command Prompt on Windows?” on page 45.
• On Mac OS X, you can start a Python interactive interpreter by double-clicking on Applications→Utilities→Terminal, and then typing python in the window that opens up.
• On Linux (and other Unixes), you might type this command in a shell or terminal window (for instance, in an xterm or console running a shell such as ksh or csh).
• Other systems may use similar or platform-specific devices. On handheld devices, for example, you might click the Python icon in the home or application window to launch an interactive session.
On most platforms, you can start the interactive prompt in additional(附加的) ways that don’t require typing a command, but they vary per platform even more widely:
• On Windows 7 and earlier, besides typing python in a shell window, you can also begin similar interactive sessions by starting the IDLE GUI (discussed later), or by selecting the “Python (command line)” menu option from the Start button menu for Python, as shown in Figure 2-1 in Chapter 2. Both spawn a Python interactive prompt with the same functionality obtained with a “python” command.
• On Windows 8, you don’t have a Start button (at least as I write this), but there are other ways to get to the tools described in the prior bullet, including tiles, Search, File Explorer(文件资源管理器), and the “All apps” interface(接口) on the Start screen. See Appendix A for more pointers on this platform.
• Other platforms have similar ways to start a Python interactive session without typing commands, but they’re too specific to get into here; see your system’s documentation for details. Anytime you see the >>> prompt, you’re in an interactive Python interpreter session—you can type any Python statement or expression here and run it immediately. We will in a moment, but first we need to get a few startup details sorted out to make sure all
readers are set to go.

Where Is Command Prompt on 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 sim- ilar 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!).

The System Path
When we typed python in the last section to start an interactive session, we relied on(依赖于) the fact that the system located (系统定位)the Python program for us on its program search path. Depending on your Python version and platform, if you have not set your system’s PATH environment variable (环境变量)to include Python’s install directory, you may need to replace the word “python” with the full path to the Python executable(执行) on your machine. On Unix, Linux, and similar, something like /usr/local/bin/python or /usr/bin/python3will often suffice(通常就足够了). On Windows, try typing C:\Python33\python(for version 3.3):

c:\code> c:\python33\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

Alternatively(另外), you can run a “cd” change-directory command to go to Python’s install directory before typing python —try the cd c:\python33 command on Windows, for
example:

c:\code> cd c:\python33 
c:\Python33> 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

But you’ll probably want to set your PATH eventually, so a simple “python” suffices(足够了 ). If you don’t know what PATH is or how to set it, see Appendix A—it covers environment variables like this whose usage (用法)varies(使多样化) per platform, as well as Python command-line arguments(参数) we won’t be using much in this book. The short story for Windows users: see the Advanced settings (高级设置)in the System entry(系统入口) of your Control Panel(控制面板). If you’re using Python 3.3 and later, this is now automatic(自动) on Windows, as the next section(部分) explains.

New Windows Options in 3.3: PATH, Launcher

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(把…放在前头):

c:\code> py
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
c:\code> py −2
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] ...
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
c:\code> py −3.1
Python 3.1.4 (default, Jun 12 2011, 14:16:16) [MSC v.1500 64 bit (AMD64)] ...
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

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.

Where to Run: Code Directories(运行:代码目录)

Now that I’ve started showing you how to run code, I want to say a few words up front about where to run code. To keep things simple, in this chapter and book at large I’m
going to be running code from a working directory (a.k.a. folder) I’ve created on my Windows computer called C:\code—a subdirectory at the top of my main drive. That’s where I’ll start most interactive sessions, and where I’ll be both saving and running most script files(脚本文件). This also means the files that examples will create will mostly show up in this directory. If you’ll be working along, you should probably do something similar before we get started. Here are some pointers if you need help getting set up with a working directory
on your computer:
• On Windows, you can make your working code directory in File Explorer or a Command Prompt window. In File Explorer, look for New Folder, see the File menu, or try a right-click. In Command Prompt, type and run a mkdir command, usually after you cd to your desired parent directory (e.g., cd c: and mkdir code ). Your working directory can be located wherever you like and called whatever you wish, and doesn’t have to be C:\code (I chose this name because it’s short in prompts). But running out of one directory will help you keep track of your work and simplify some tasks. For more Windows hints, see this chapter’s sidebar on Command Prompt, as well as Appendix A.
• On Unix-based systems (including Mac OS X and Linux), your working directory might be in /usr/home and be created by a mkdir command in a shell window or file explorer GUI specific to your platform, but the same concepts apply. The Cyg-win Unix-like system for Windows is similar too, though your directory names may vary (/home and /cygdrive/c are candidates). You can store your code in Python’s install directory too (e.g., C:\Python33 on Windows) to simplify some command lines before setting PATH , but you probably shouldn’t—this is for Python itself, and your files may not survive a move or uninstall.
Once you’ve made your working directory, always start there to work along with the examples in this book. The prompts in this book that show the directory that I’m
running code in will reflect my Windows laptop’s working directory; when you see C:\code> or % , think the location and name of your own directory.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值