Python All-in-One For Dummies
Book image
Explore Book Buy On Amazon
You'll hear the word module used in conjunction with Python all the time. If you think of the Python standard library as an actual physical library, and a package as being, perhaps, one book in that library, then you can think of a Python module as being one chapter in one book.

In other words, a package may contain many modules, and a library may contain many packages. The module is a big part of what makes Python a modular language, because code is grouped together according to function. So in the one hand, you don’t have to import everything including the kitchen sink to use some code. By the same token, if you need to use several related things, such as functions for working with dates and times, you don’t have to import them all one at a time.

Typically, importing just the whole module will get you what you need. For example, to work with dates and times, you don’t have to import every Python module out there. Nor do you need to import every possible little function and object one at a time. You just import the whole module, like datetime, to get lots of handy things for working with dates and times.

There are actually a few ways to import functionality from modules. One of the most common is to simply import the whole Python module. To do that, you just follow the import command with the name of the Python module you want to import. For example, this imports the entire math module, which has lots of functions and stuff for doing math:

import math
After you import a Python module, the dir() and help() functions work on that too. For example, if you tried doing dir(math) or help(math) before import math, you'd get an error. That’s because that math package isn’t part of the standard library. However, if you do import math first, and then help(math), then it all works.

There may be times where you don't really need the whole kit-and-caboodle. In those cases, you can import just what you need using a syntax like this:

from math import pi
In this example, you’re just importing one thing (pi), so you’re not bringing in unnecessary stuff. The second example is also handy because in your code you can refer to pi as just pi, you don't have to use math.pi. Here is some stuff you can try at the Python prompt, such as in a VS Code Terminal window, to see for yourself:

Enter the command print(pi) and press Enter. Most likely you'll get an error that reads:

NameError: name 'pi' is not defined
In other words, pi isn’t part of the standard library that’s always available to your Python code. To use it, you have to import the math module. There are two ways to do that. You can import the whole thing by typing this at the Python prompt:
import math
But if you do that and then enter
print(pi)
… you'll get the same error again, even though you imported the math package. The reason for that is when you import an entire Python module and you want to use a part of it, you have to precede the part you want to use with the name of the module and a dot. For example, if you enter this command:
print(math.pi)
… you get the correct answer:
3.141592653589793
Be aware that when you import just part of a Python module, the help() and dir() functions for the whole module won't work. For example, if you’ve only executed from math import pi in your code and you attempt to execute a dir(math) or help(math) function, it won't work, because Python doesn’t have the entire module at its disposal. It only has at its disposal that which you imported, pi in this example.

Usually help() and dir() are just things you use at the Python prompt for a quick lookup. They're not the kinds of things you’re likely to use when actually writing an app. So using from rather than import is actually more efficient because you're only bringing in what you need. As an added bonus, you don’t have to precede the function name with the module name and a dot. For example, when you import only pi from the math module, like this:

from math import pi
Then you can refer to pi in your code is simply pi, not math.pi. In other words, if you execute this function:
print(pi)
… you'll get the right answer:
3.141592653589793
This is because Python now “knows” of pi as being the thing named pi from the math module; you don't have to specifically tell it to use math.pi.

You can also import multiple items from a package by listing their names, separated by commas, at the end of the from … command. For example, suppose you need pi and square roots in your app. You could import just those into your app using this syntax:

from math import pi, sqrt
Once again, because you used the from syntax for the import, you can refer to pi and sqrt() in your code by name without the leading module name. For example, after executing that from statement, this code:
print(sqrt(pi))
… displays
1.7724538509055159
… which, as you may have guessed, is the square root of the number pi.

You may also notice people importing a Python module like this:

from math import *
The asterisk is short for “everything.” So in essence, that command is exactly the same as import math, which also imports the entire math module. But this is a subtle difference: When you do from math import * you associate the name of everything in the math module with that Python module. So you can use those names without the math. prefix. In other words, after you execute this:
from math import *<?pre>
… you can do a command like print(pi) and it will work, even without using print(math.pi). Although it does seem like a smart and convenient thing to do, many Python programmers think that sort of thing isn't very Pythonic. If you’re importing lots of Python modules and using lots of different pieces of each, avoiding module names in code can make it harder for other programmers to read and make sense of that code. So although in a Zen of Python sense it may be frowned upon, technically it works and is an option for you.

About This Article

This article is from the book:

About the book authors:

John Shovic, PhD, is a computer science faculty member at the University of Idaho specializing in robotics and artificial intelligence.

Alan Simpson is a web development professional who has published more than 100 articles and books on technology.

Alan Simpson is the author of over 90 computer books on databases, Windows, Web site design and development, programming, and networking. His books are published throughout the world in over a dozen languages and have millions of copies. Alan has also taught introductory and advanced computer programming courses at San Diego State University and the UCSD Extension. He has served as a consultant on high-technology, educationoriented projects for the United States Navy and Air Force. Despite that, Alan has no fancy job title because he has never had a real job.

This article can be found in the category: