0Pricing
Python Academy · Lesson

Creating and Importing Modules

Write your own modules and import them into other scripts.

Creating and Importing Modules is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

A module is a Python file containing functions, classes, and variables that can be imported into other scripts.

What is a Module?

Any .py file is a module. utils.py can be imported as: import utils. Python searches sys.path for modules.
import sys
print(sys.path[:3])

import Statement

import math imports the whole module. Access with math.sqrt(16). The module is cached after the first import.
import math
print(math.sqrt(16))
print(math.pi)

from ... import

from math import sqrt, pi imports specific names into the local namespace. No need for the math. prefix.
from math import sqrt, pi
print(sqrt(25))
print(pi)

import as (aliases)

import numpy as np gives the module a shorter local name. Useful for long module names.
import os.path as osp
print(osp.join('a', 'b', 'c'))

from ... import *

from math import * imports everything. Avoid it — it pollutes the namespace and makes it hard to know where names come from.
# Avoid:
# from math import *
# Use explicit imports instead
print('namespace tip')

__name__ == '__main__'

if __name__ == '__main__': runs only when the file is executed directly, not when imported as a module.
# In your script:
# if __name__ == '__main__':
#     main()
print(__name__)

Reloading Modules

importlib.reload(module) re-imports a changed module without restarting. Useful in interactive sessions.
import importlib, math
importlib.reload(math)
print('reloaded')

dir() and help()

dir(math) lists all names in the module. help(math.sqrt) shows the docstring.
import math
print(dir(math)[:5])

Module Search Path

Python looks in: script directory, PYTHONPATH, installation default. Add paths with sys.path.insert(0, '/mydir').
import sys
sys.path.insert(0, '/tmp')
print(sys.path[:3])

__all__ Variable

Define __all__ = ['func1', 'func2'] in a module to control what from module import * exports.
# In mymodule.py:
# __all__ = ['public_func']
# def public_func(): ...
# def _private(): ...
print('__all__ demo')

Quick Check

What does if __name__ == '__main__': guard against?

Recap

Modules are .py files. Use import or from...import. if __name__=='__main__': guards entry points. dir() lists names. sys.path controls search order.

Keep Going

Excellent! Continue to the next lesson to deepen your skills.

Frequently asked questions

Is the “Creating and Importing Modules” lesson free?

Yes — the full text of “Creating and Importing Modules” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Creating and Importing Modules”?

Write your own modules and import them into other scripts. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating and Importing Modules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Importing Modules
  2. The Python Standard Library
  3. Packages and __init__.py
  4. Installing Packages with pip
← Back to Python Academy