- Python Shortcuts, Commands, and Packages
- 4.2 Twenty-Two Programming Shortcuts
- 4.3 Running Python from the Command Line
- 4.4 Writing and Using Doc Strings
- 4.5 Importing Packages
- 4.6 A Guided Tour of Python Packages
- 4.7函数作为一类对象
- 4.8 Variable-Length Argument Lists
- 4.9 Decorators and Function Profilers
- 4.10 Generators
- 4.11 Accessing Command-Line Arguments
- Chapter 4 Summary
- Chapter 4 Questions for Review
- Chapter 4 Suggested Problems
4.5 Importing Packages
Later sections in this chapter, as well as later chapters in the book, make use of packages to extend the capabilities of the Python language.
Apackageis essentially a software library of objects and functions that perform services. Packages come in two varieties:
Packages included with the Python download itself. This includesmath,random,sys,os,time,datetime, andos.path。这些包是特别方便,因为e no additional downloading is necessary.
Packages you can download from the Internet.
The syntax shown here is the recommended way to an import a package. There are a few variations on this syntax, as we’ll show later.
importpackage_name
For example:
import math
Once a package is imported, you can, within IDLE, get help on its contents. Here’s an example:
>>>import math>>>help(math)
If you type these commands from within IDLE, you’ll see that the math package supports a great many functions.
But with this approach, each of the functions needs to be qualified using the dot (.) syntax. For example, one of the functions supported issqrt(square root), which takes an integer or floating-point input.
>>>math.sqrt(2)1.4142135623730951
You can use themathpackage, if you choose, to calculate the value ofpi。然而,the math package also provides this number directly.
>>>math.atan(1) * 43.141592653589793 >>>math.pi3.141592653589793
Let’s look at one of the variations on theimportstatement.
importpackage_name[asnew_name]
In this syntax, the brackets indicate that theasnew_nameclause is optional. You can use it, if you choose, to give the package another name, oralias, that is referred to in your source file.
This feature provides short names if the full package name is long. For example, Chapter 13 introduces thematplotlib.pyplotpackage.
import matplotlib.pyplot as plt
Now, do you want to use the prefixmatplotlib.pyplot, or do you want to prefix a function name withplt? Good. We thought so.
Python supports other forms of syntax for theimportstatement. With both of these approaches, the need to use the package name and the dot syntax is removed.
frompackage_nameimportsymbol_namefrompackage_nameimport *
In the first form of this syntax, only thesymbol_namegets imported, and not the rest of the package. But the specified symbol (such aspiin this next example) can then be referred to without qualification.
>>>from math import pi>>>print(pi)3.141592653589793
This approach imports only one symbol—or a series of symbols separated by commas—but it enables the symbolic name to be used more directly. To import an entire package, while also gaining the ability to refer to all its objects and functions directly, use the last form of the syntax, which includes an asterisk (*).
>>>from math import *>>>print(pi)3.141592653589793 >>>print(sqrt(2))1.4142135623730951
The drawback of using this version ofimportis that with very large and complex programs, it gets difficult to keep track of all the names you’re using, and when you import packages without requiring a package-name qualifier, name conflicts can arise.
So, unless you know what you’re doing or are importing a really small package, it’s more advisable to import specific symbols than use the asterisk (*).