Lesson 41 of 60
โฎPrev Nextโฏ

PY-101 ยท Modules and packages

import and from

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

41of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    import brings a module into your program, and you use its contents with a dot: import math, then math.sqrt(16). from math import sqrt imports one name so you can call sqrt directly, and as gives a module a shorter nickname. Modules let you reuse code instead of writing everything yourself.

    • Import a whole module
    • Import specific names with from
    • Rename with as

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    import and from, running
    1. import math
    2. from statistics import mean, median
    3. import datetime as dt
    4. print(math.sqrt(16), round(math.pi, 2))
    5. marks = [72, 90, 85, 64]
    6. print("Mean:", mean(marks), "Median:", median(marks))
    7. print(dt.date(2026, 12, 1).isoformat())
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou need a square root, an average and today's date. Writing those yourself would take an afternoon; Python already ships them, one import away.
    2. The Python ideaimport moduleimport brings a module into your program, and you use its contents with a dot: import math, then math.sqrt(16).
    3. In real lifeA typical data script starts with import pandas as pd, import numpy as np and import matplotlib.pyplot as plt.

    Start with a question

    You need a square root, an average and today's date. Writing those yourself would take an afternoon; Python already ships them, one import away.

    Real programs are built from modules: Python's own, other people's from PyPI, and your own files.

    Explain it like I'm 10

    import is borrowing a toolbox from the shed. from ... import is borrowing just the hammer.

    The technical explanation

    import math then math.pi keeps the module name, so it is obvious where pi came from.

    from statistics import mean, median imports just those two names.

    import datetime as dt lets you write dt.date instead of the full name. pandas is almost always import pandas as pd.

    Syntax

    import module
    from module import name
    import module as short

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A typical data script starts with import pandas as pd, import numpy as np and import matplotlib.pyplot as plt.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. After import math, how do you call sqrt?
    2. 2. After from math import pi, how do you use pi?
    3. 3. What does import numpy as np do?
    4. 4. Why avoid from x import *?
    5. 5. What is a module?

    Questions people ask

    What is the difference between a module and a package in Python?

    A module is one .py file. A package is a folder of modules (traditionally with an __init__.py) that you import as a group, such as email or json.

    Where does Python look for modules?

    In the folder of the running script, then the standard library, then installed packages. sys.path lists the exact folders.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between a module and a package in Python?

    A module is one .py file. A package is a folder of modules (traditionally with an __init__.py) that you import as a group, such as email or json.

    beginner Where does Python look for modules?

    In the folder of the running script, then the standard library, then installed packages. sys.path lists the exact folders.

    intermediate After import math, how do you call sqrt?

    math.sqrt(9). Use the module name and a dot.

    intermediate After from math import pi, how do you use pi?

    pi. The name is imported directly.

    Quick revision

    Remember

    • Import a whole module
    • Import specific names with from
    • Rename with as
    • Avoid from module import *. It dumps every name into your program and you can no longer tell where something came from.

    Syntax

    import module
    from module import name
    import module as short

    Official documentation