Home > Articles

  • Print
  • + Share This
This chapter is from the book

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.

A package is essentially a software library of objects and functions that perform services. Packages come in two varieties:

  • Packages included with the Python download itself. This includes math, random, sys, os, time, datetime, and os.path. These packages are especially convenient, because 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.

key.jpg
import package_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 is sqrt (square root), which takes an integer or floating-point input.

>>> math.sqrt(2)
1.4142135623730951

You can use the math package, if you choose, to calculate the value of pi. However, the math package also provides this number directly.

>>> math.atan(1) * 4
3.141592653589793
>>> math.pi
3.141592653589793

Let’s look at one of the variations on the import statement.

key.jpg
import package_name [as new_name]

In this syntax, the brackets indicate that the as new_name clause is optional. You can use it, if you choose, to give the package another name, or alias, 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 the matplotlib.pyplot package.

import matplotlib.pyplot as plt

Now, do you want to use the prefix matplotlib.pyplot, or do you want to prefix a function name with plt? Good. We thought so.

Python supports other forms of syntax for the import statement. With both of these approaches, the need to use the package name and the dot syntax is removed.

from package_name import symbol_name
from package_name import *
key.jpg

In the first form of this syntax, only the symbol_name gets imported, and not the rest of the package. But the specified symbol (such as pi in 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 of import is 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 (*).

  • + Share This
  • 🔖 Save To Your Account