While learning programming and reading some resources you undoubtedly came across the word _abstraction_, which simply means to reduce and reuse code as much as possible.
Modules come into the picture when you want to reuse a group of functions in different source files. Modules are also useful in structuring the program well.
Sometimes third party modules do not come bundled with Python, but need to be installed using package managers like [pip](https://bootstrap.pypa.io/get-pip.py), [easy install](https://bootstrap.pypa.io/ez_setup.py), and [pipenv](https://github.com/pypa/pipenv).
We want to make a program that has various functions regarding prime numbers. So lets start. We will define all the functions in `prime_functions.py`:
```python
# prime_functions.py
from math import ceil, sqrt
def isPrime(a):
if a == 2:
return True
elif a % 2 == 0:
return False
else:
for i in range(3,ceil(sqrt(a)) + 1,2):
if a % i == 0:
return False
return True
def print_n_primes(a):
i = 0
m = 2
while True:
if isPrime(m) ==True:
print(m)
i += 1
m += 1
if i == a:
break
```
Now we want to use the functions that we just created in `prime_functions.py`, so we create a new file called `playground.py` to use those functions.
> _Please note that this example is far too simple to need two separate files -- it is just to demonstrate. But when there are large, complex programs, splitting the code into different files is really useful._
A good practice is to sort `import` modules into three groups - standard library imports, related third-party imports, and local imports. Within each group it is sensible to sort alphabetically by module name. You can find [more information in PEP8](https://www.python.org/dev/peps/pep-0008/?#imports).
```python
# Order of importing modules
import time # Standard library modules
from package import calculate_duration # Related third-party modules
from package.sibling import example # Local modules within the project
One of the most important things to keep in mind when writing Python is legibility, and alphabetically sorting modules makes them faster to read and search through. Also, it is easier to verify that something is imported, and avoid duplicated imports.