Table of Contents
IronForgeCyber
Courses
Resources
Blog
Log In


IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. python
  3. modules
5 min read

Modules

A module is a file containing Python code. Modules can be Python files written by you, someone else, or built-in modules. Modules contain functions, classes, and variables that can be used throughout a project. Writing modular code helps write code that's easier to maintain, grow, and manage.

Modules are added using the import statement followed by the name of the module. The name of the module is the name of the file without .py; therefore, special_operations.py is referenced as special_operations.

python
1import special_operations
2
3list_product = special_operations.prod([1, 2, 3])

You can use from x import y syntax to only import certain elements from a module.

python
1from special_operations import prod, avg
2
3list_prod = prod([1, 2, 3])
4list_avg 

You can use import x as y to change the name of an import. This is common for longer import names or if multiple imports have the same name. Here are some of the most common import shorthands:

python
1import numpy as np
2import matplotlib.pyplot as plt
3import networkx as nx
4import pandas as pd
5

Packages and Relative Imports

A directory of modules is a package, and modules inside it are imported with dotted names. Consider the following directory setup:

mainfolder:
    mainfile.py
    subfolder1:
        file1.py
    subfolder2:
        file2.py

From the directory that contains mainfolder, we can import any of these files with an absolute import:

python
1import mainfolder.mainfile
2import mainfolder.subfolder1.file1
3from mainfolder.subfolder2 import file2

Inside the package, a module can also use a relative import (PEP 328), where a leading dot means "this package" and two dots mean "the parent package". From file1.py:

python
from ..subfolder2 import file2
Warning
Relative imports only work in a module imported as part of a package. Running python3 file1.py directly fails with ImportError: attempted relative import with no known parent package; run it as python3 -m mainfolder.subfolder1.file1 from the top directory instead.

Listing Module Items

The built-in dir() function shows what a module defines.

For example, consider the math module:

python
1>>> import math
2>>> dir(math)
3['__doc__', 

Running a Module as a Script

Importing a module runs it. Every top-level statement in the file executes the first time it's imported, the same as if you had run the file directly. That's what defines its functions and variables, but it also runs anything else at the top level, such as a print or the code that solves a challenge.

Say special_operations.py ends with a quick test of its own functions:

special_operations.py
1def prod(nums):
2    result = 1
3    for n in nums:
4        result *= n

Now every file that runs import special_operations prints 6 as a side effect. To keep code from running on import, Python gives every module a variable called __name__. When a file is run directly, its __name__ is the string "__main__"; when it's imported, __name__ is the module's name. Checking it separates the two cases:

special_operations.py
1def prod(nums):
2    result = 1
3    for n in nums:
4        result *= n

Running python3 special_operations.py still prints 6, but importing it no longer does. Most scripts follow this shape, often with the top-level work moved into a main() function:

python
1def main():
2    ...
3
4if __name__ == "__main__":
5
Tip
Writing solve scripts this way pays off quickly. A pytest file can import your parsing functions and test them without running the whole solution.

A module can also be run by its import name instead of its file path, using python3 -m. This runs it with __name__ set to "__main__", and it's how you run a module inside a package, as in the relative import warning above. Many standard-library modules are useful run this way:

$ python3 -m http.server 8000Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

That one-liner serves the current directory over HTTP, a quick way to move files onto a target machine during a CTF.

=
avg
(
[
1
,
2
,
3
]
)
from
datetime
import
datetime
as
dt
'__file__'
,
'__loader__'
,
'__name__'
,
'__package__'
,
'__spec__'
,
'acos'
,
'acosh'
,
'asin'
,
'asinh'
,
'atan'
,
'atan2'
,
'atanh'
,
'ceil'
,
'comb'
,
'copysign'
,
'cos'
,
'cosh'
,
'degrees'
,
'dist'
,
'e'
,
'erf'
,
'erfc'
,
'exp'
,
'expm1'
,
'fabs'
,
'factorial'
,
'floor'
,
'fmod'
,
'frexp'
,
'fsum'
,
'gamma'
,
'gcd'
,
'hypot'
,
'inf'
,
'isclose'
,
'isfinite'
,
'isinf'
,
'isnan'
,
'isqrt'
,
'lcm'
,
'ldexp'
,
'lgamma'
,
'log'
,
'log10'
,
'log1p'
,
'log2'
,
'modf'
,
'nan'
,
'nextafter'
,
'perm'
,
'pi'
,
'pow'
,
'prod'
,
'radians'
,
'remainder'
,
'sin'
,
'sinh'
,
'sqrt'
,
'tan'
,
'tanh'
,
'tau'
,
'trunc'
,
'ulp'
]
5
return
result
6
7print(prod([1, 2, 3]))
5 return result
6
7if __name__ == "__main__":
8 print(prod([1, 2, 3]))
main
(
)