Home / Python Fundamentals / Imports & Modules
Lesson 7 of 8

Imports & Modules

Concept

Python organizes code into modules (files) and packages (directories with __init__.py). import brings in code from other files. Your app/ directory is a package — each .py file is a module.
m3shdup/app/intrinsic.py
# Standard library (comes with Python)
import os
import logging
import random

# From a specific module, import specific things
from datetime import datetime, timezone

# From your own package
from . import db          # same package (app/db.py)
from .dispatch import dispatch_task  # specific function

What's happening

import os gives you the whole module — use as os.environ. from datetime import datetime pulls out just what you need — use directly as datetime.now(). from . import db — the dot means 'current package' (the app/ directory).

Quick Check — 3 questions

1. What does from . import db mean?

2. After import os, how do you access environment variables?

3. What makes a directory a Python package?