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.

Importing a module

Watch other topic trails
02:02

4 ways to import a module in Python
04:09

Importing a module runs code
03:16

Modules are cached
02:36

Importing everything from a module
04:22

Related article:

Let's talk about importing modules in Python.

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 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'>
```

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.

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.

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`

.

Hello friendly web visitor! ðŸ‘‹

This page is part of Python Morsels, an online Python skill-building service.

The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.

Python Morsels topics pages are free and **the first month's worth of exercises is free as well**.
You don't need to enter payment details to sign up.

You can find explanations of many other Python topics by signing up below.

By signing up, you agree to the Privacy Policy.