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, likedatetime
, 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 mathAfter 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 piIn 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 definedIn 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 mathBut 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.141592653589793Be 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 piThen 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.141592653589793This 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, sqrtOnce 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.