Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 12 章 模块和软件包 模块和包

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

关于软件包的信息和软件包本身一样重要。

弗雷德里克-W-史密斯

在 自下而上的攀登过程中,您已经从内置数据类型发展到构建越来越大的数据和代码结构。 在本章中,您终于学会了用 Python 编写实际的完整程序。 您将编写自己的模块,并学习如何使用 Python 标准库和其他来源的模块。

本书是按层次组织的:单词、句子、段落和章节。 否则,它很快就会变得难以阅读。1 代码也有大致类似的自下而上的组织结构:数据类型就像单词;表达式和语句就像句子;函数就像段落;模块就像章节。 继续类比,当我说本书第 8 章解释了某些内容时,在编程中就像提到了另一个模块中的代码。

模块和 import 语句

我们会在不止一个 文件中创建和使用 Python 代码。模块就是任何 Python 代码的文件。您不需要做任何特别的事情;任何 Python 代码都可以被其他人用作模块。

我们通过使用 Pythonimport 语句来引用其它模块的代码。这样,您的程序就可以使用导入模块中的代码和变量。

导入模块

Python 允许您以不同的方式从不同的模块导入代码:

  • import 语句最简单的用法是import module,其中 module是另一个 Python 文件的名称,不带.py扩展名。

  • 要只访问名为wonders 的模块中的函数do_something() ,请使用from wonders import do_something

  • 要访问wonders 中的所有内容,请使用from wonders import * 。通常不建议这样做,因为你会得到,嗯,所有内容,因为变量现在散落在你的调用程序中,可能包括你不知道或不想要的东西。

比方说,你和其他几个人想吃快餐,但又不想进行长时间的讨论,结果总是选了最吵的那个人想要的东西。 让计算机来决定吧!让我们编写一个模块,其中包含一个返回随机快餐选择的函数,以及一个调用它并打印选择的主程序。 Python 标准库包含一个名为 random.py 的模块,其中有一个名为choice() 的函数:

例 12-1 显示了导入它的模块。

例 12-1.
from random import choice

places = ["McDonalds", "KFC", "Burger King", "Taco Bell",
     "Wendys", "Arbys", "Pizza Hut"]

def pick():  # see the docstring below?
    """Return random fast food place"""
    return choice(places)

例 12-2显示了导入该模块的主程序。

例 12-2.
import fast

place = fast.pick()
print("Let's go to", place)

如果将这两个文件放在同一目录下,并指示 Python 作为主程序运行lunch.py,Python 将访问fast 模块并运行它的pick() 函数。我们编写的这个版本的pick()是为了从字符串列表中返回一个随机结果,所以这就是主程序将返回并打印的结果:

$ python lunch.py
Let's go to Burger King
$ python lunch.py Let's go to Pizza Hut ...
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

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898