Importing a module in Python

Share
Copied to clipboard.
Series: Modules
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
02:02

Let's talk about importing modules in Python.

Importing a module

Python comes bundled with a whole bunch of modules called the Python standard library. We're going to import the math module from the standard library:

>>> import math

The math module has a function called sqrt that we can use to get the square root of a number.

We can't currently call the sqrt function directly:

>>> sqrt(25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined

To use the sqrt function we need to call math.sqrt:

>>> math.sqrt(25)
5.0

Whenever you import a module, Python will make just one variable, the name of the module that we imported, (math in our case) which points to a module object:

>>> math
<module 'math' from '/usr/lib/python3.9/lib-dynload/math.cpython-39-x86_64-linux-gnu.so'>

Module object

When we imported the math module, we got a module object, and that module object has attributes.

Our math module object has a pi attribute:

>>> math.pi
3.141592653589793

And an e attribute:

>>> math.e
2.718281828459045

And a sqrt attribute:

>>> math.sqrt
<built-in function sqrt>
>>> math.sqrt(25)
5.0

And a whole bunch of other attributes. Everything within the math module lives as an attribute on that math module object.

Importing specific module elements

After you've imported the math module, you'll need to put math. before the name of anything you'd like to use in the module. If you don't want to have to type math. something every time you use something in the math module, instead of using an import statement you could use a from-import statement.

So instead of this:

>>> import math

You could do this:

>>> from math import sqrt

Now we'll have access to the sqrt function directly:

>>> sqrt(25)
5.0

Or if we wanted to import multiple things we could put commas between them:

>>> from math import sqrt, pi

We now have pi and sqrt:

>>> pi
3.141592653589793
>>> sqrt(25)
5.0

but we don't have e:

>>> e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'e' is not defined

Because we didn't import e from the math module.

See 4 ways to import a module in Python for more on importing.

Use import X or from X import Y to import modules in Python

If you want to import a module in Python, you'll need to use an import statement. But unless you want to type the name of the module over and over (each time you access something in the module), you might want to use the from syntax for importing instead: from MODULE_NAME import THINGS_FROM_MODULE.

Series: Modules

Modules are the tool we use for breaking up our code into multiple files in Python. When you write a .py file, you're making a Python module. You can import your own modules, modules included in the Python standard library, or modules in third-party packages.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video
02:02