Kahibaro
Discord Login Register

Importing modules

Why importing matters

Python becomes much more powerful when you use modules: files (or packages) that contain extra functions, classes, and variables you can use in your own programs.

In this chapter, you’ll learn the different ways to import modules and how those choices affect how you write and read code.

We’ll use the standard library modules like math and random for examples, but the focus here is how to import, not what each module does.


The basic `import` statement

The simplest way to use a module is:

import math
print(math.sqrt(16))
print(math.pi)

What happens:

General pattern:

import module_name
result = module_name.function_name(arguments)
value = module_name.variable_name

You can import multiple modules on one line:

import math, random
print(math.pi)
print(random.randint(1, 10))

This is valid, but many style guides prefer one module per line for readability:

import math
import random

Importing with an alias: `import ... as ...`

Sometimes module names are long, or you want a shorter, more convenient name. Use as:

import math as m
print(m.sqrt(25))
print(m.pi)

You can also alias other modules:

import random as rnd
num = rnd.randint(1, 6)
print(num)

This does not change the module itself, only the name you use in your code.

Why aliases are useful:

Importing specific names: `from module import name`

If you only need a few things from a module, you can import them directly:

from math import sqrt, pi
print(sqrt(16))
print(pi)

Notice:

General pattern:

from module_name import name1, name2, name3
result = name1(...)

You can also use aliases with this form:

from math import sqrt as square_root, pi as PI
print(square_root(9))
print(PI)

This is helpful when:

`import module` vs `from module import ...`

Both forms work, but they have different trade-offs.

`import module`

import math
print(math.sqrt(9))

Pros:

Cons:

`from module import name`

from math import sqrt
print(sqrt(9))

Pros:

Cons:

A common beginner-friendly approach:

Importing everything: `from module import *` (and why to avoid it)

You may see:

from math import *

This imports all public names from math into your current file. After this, you can directly use sqrt, pi, cos, etc., without math..

Example:

from math import *
print(sqrt(16))
print(pi)

Problems with import *:

Most style guides say: avoid from module import * in real programs, especially as a beginner. Use it only in short experiments or interactive sessions if you understand the risks.


Where to write import statements

Common practice is:

Example:

# Simple program to demonstrate imports
import math
import random as rnd
from datetime import date
today = date.today()
print("Today's date:", today)
angle = math.pi / 4
print("Random number:", rnd.random())
print("cos(angle) =", math.cos(angle))

Why:

Importing your own modules (files)

You are not limited to built-in or installed modules. A Python file you write can also be used as a module.

Suppose you have two files in the same folder:

helpers.py:

def greet(name):
    print(f"Hello, {name}!")
PI = 3.14159

main.py:

import helpers
helpers.greet("Alice")
print(helpers.PI)

Here:

You can also import specific names:

from helpers import greet, PI
greet("Bob")
print(PI)

Important:

What happens when you import (once-only behavior)

When Python imports a module:

  1. It runs the module file from top to bottom once.
  2. It stores the loaded module in memory.
  3. If you import the same module again in the same program, Python reuses the already-loaded version instead of running the file again.

Example:

utils.py:

print("utils module is being imported")
def add(a, b):
    return a + b

main.py:

import utils
import utils  # imported again
print(utils.add(2, 3))

Output:

utils module is being imported
5

Note that the message appears only once, even though import utils is written twice. This is normal and helpful for performance.


The `__name__ == "__main__"` pattern (very briefly)

When you start making your own modules, you might see this pattern:

def main():
    print("Running main function")
if __name__ == "__main__":
    main()

Here’s the idea as it relates to imports:

This lets a file act both as:

You’ll use this more when you start structuring bigger programs.


Import errors and how to handle them

If Python can’t find a module, you’ll see an error:

import not_a_real_module

Error message:

ModuleNotFoundError: No module named 'not_a_real_module'

Common reasons:

For standard library modules like math, random, datetime, you don’t need to install anything; they come with Python. For external modules, you’ll use tools like pip (covered elsewhere) to install them before importing.


Summary

Views: 18

Comments

Please login to add a comment.

Don't have an account? Register now!