Packages and Import Statements

A package is a way of organizing related modules together inside a directory, and the import statement is how you bring the code from a module or package into your current script so you can use it.

Basic Import Syntax

import math
print(math.pi)          # access via the module name

from math import pi
print(pi)                # import a specific name directly

from math import pi as PI
print(PI)                 # import with an alias

import math as m
print(m.pi)                # alias the whole module

What Is a Package?

A package is simply a directory containing multiple related module files, along with a special __init__.py file that tells Python to treat that directory as a package. This lets large projects organize hundreds of files logically instead of dumping everything into one folder.

my_project/
    __init__.py
    data_cleaning.py
    visualization.py
    models.py
from my_project import data_cleaning
data_cleaning.clean(data)
Using from module import * to import everything from a module is generally discouraged, since it makes it unclear where each function or variable actually came from, and can accidentally overwrite names already in your script.

Coming Up Next

Next, you'll learn about the reload function - useful when you need Python to re-read a module that has changed during an active session.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course