What Are Packages in Python? Absolute Beginners Tutorial

Python packages are collections of reusable code that other developers have written and made available for you to use in your own programs. Learning how packages work — and how to find, install, and import them — is one of the most practical skills you can pick up as a beginner.

When you first start writing Python, you write everything yourself in a single file. That works fine for small scripts, but programs quickly grow to the point where keeping all your code in one place becomes difficult to manage. Python's solution to this is the module and package system — a way to split code across files and directories, and to pull in code written by others without rewriting it from scratch.

What Is a Module?

Before understanding packages, you need a clear picture of what a module is. A module is simply a Python file — any file that ends in .py. If you create a file called greetings.py and write a function inside it, that file is a module. Another Python file can then load and use that function by importing it.

python
# greetings.py  — this file is a module

def say_hello(name):
    return f"Hello, {name}!"

def say_goodbye(name):
    return f"Goodbye, {name}!"

Another file in the same folder can import that module and call its functions:

python
# main.py

import greetings

print(greetings.say_hello("Maya"))   # Hello, Maya!
print(greetings.say_goodbye("Maya")) # Goodbye, Maya!

You can also import only the specific function you need using the from keyword:

python
from greetings import say_hello

print(say_hello("Carlos"))  # Hello, Carlos!
Note

When you use import greetings, you access functions as greetings.say_hello(). When you use from greetings import say_hello, you call it directly as say_hello() — no prefix needed. Neither approach is wrong; pick whichever makes your code easier to read.

code builder click a token to place it

Build a valid import statement that loads only the sqrt function from the math module:

your code will appear here...
import sqrt from math as include
Why: The correct form is from math import sqrt. The keyword from specifies the module name, and import specifies the name to bring in. The distractor as is used for aliasing an import (like import math as m), not for selecting a specific function. The word include does not exist as a Python keyword.

What Is a Package?

A package is a directory — a folder — that contains one or more module files. What makes a directory a Python package rather than just a folder is the presence of a file called __init__.py inside it. That file can be completely empty; its presence alone signals to Python that this directory should be treated as a package.

Here is what a simple package structure looks like on disk:

text
my_project/
├── main.py
└── utils/
    ├── __init__.py
    ├── math_helpers.py
    └── text_helpers.py

In this layout, utils is a package. It contains two modules: math_helpers and text_helpers. You import from that package like this:

python
# From main.py, import a function from the utils package
from utils.math_helpers import round_to_two

result = round_to_two(3.14159)
print(result)  # 3.14
Pro Tip

In Python 3.3 and later, Python supports something called "namespace packages," which means a directory without __init__.py can still function as a package in certain situations. For everyday work, though, including an __init__.py is the clearer and safer choice.

Packages can also be nested. A package can contain sub-packages, which themselves contain modules. This is how large libraries like email and xml in Python's standard library are organized — many layers of packages and sub-packages, each with a focused purpose.

What it is
A single .py file containing Python code — functions, classes, or variables.
Example
math.py, greetings.py, or any file you create ending in .py.
What it is
A directory of modules (and possibly sub-packages) that includes an __init__.py file.
Example
email/, xml/, or any folder with __init__.py inside it.
What it is
An informal term for a collection of related packages and modules distributed together. Not a technical Python term — just common usage.
Example
requests, NumPy, and Django are all libraries made up of multiple packages and modules.

The Python Standard Library

Python ships with a large collection of modules and packages built right in. This collection is called the standard library. You do not need to install anything to use it — if you have Python, you have the standard library. You simply import what you need.

Here are a few of the standard library modules beginners use most often:

python
# math — mathematical functions
import math
print(math.sqrt(25))      # 5.0
print(math.pi)            # 3.141592653589793

# random — random number generation
import random
print(random.randint(1, 10))  # a random integer between 1 and 10

# datetime — working with dates and times
import datetime
today = datetime.date.today()
print(today)              # e.g. 2025-04-03

# os — interacting with the operating system
import os
print(os.getcwd())        # prints the current working directory

# json — reading and writing JSON data
import json
data = {"name": "Alex", "age": 30}
print(json.dumps(data))   # '{"name": "Alex", "age": 30}'
"The standard library contains built-in modules written in C that provide access to system functionality." — Python Documentation

The standard library is extensive — it covers file I/O, networking, compression, regular expressions, email, cryptography, and much more. Before reaching for a third-party package, it is worth checking whether the standard library already has what you need.

spot the bug click the line that contains the bug

This code tries to use the standard library to get the current date and print it. One line contains a bug. Click the line you think is wrong, then hit check.

1 import datetime
2
3 today = datetime.today()
4 print(today)
The fix: Change datetime.today() to datetime.date.today(). The datetime module contains a class also called datetime and a separate class called date. Calling datetime.today() on the module itself raises an AttributeError. You need to navigate to the date class first: datetime.date.today().

Third-Party Packages and PyPI

The standard library is powerful, but it does not cover everything. The wider Python community has published hundreds of thousands of open-source packages for tasks the standard library does not address — web scraping, machine learning, HTTP requests, image processing, data analysis, and far more.

These packages live in a public repository called the Python Package Index, commonly referred to as PyPI (pronounced "pie-pee-eye"). PyPI is hosted at pypi.org. When someone publishes a Python package, they upload it to PyPI so others can find and install it.

Some of the third-party packages that beginners encounter most often include:

Purpose
Makes sending HTTP requests simple. Used to fetch data from APIs and web pages.
Install
pip install requests
Purpose
Provides tools for working with tabular data (like spreadsheets) in Python. Widely used in data science.
Install
pip install pandas
Purpose
A lightweight web framework for building web applications and APIs.
Install
pip install flask
Purpose
Adds support for large multi-dimensional arrays and high-performance mathematical operations. The foundation of scientific computing in Python.
Install
pip install numpy

Installing Packages with pip

pip is the tool Python uses to install packages from PyPI. It has been included automatically with Python since version 3.4, so in nearly all cases you already have it. You run pip commands in your terminal (also called the command prompt or shell) — not inside a Python script.

Here are the pip commands you will use most often as a beginner:

terminal
# Install a package
pip install requests

# Install a specific version
pip install requests==2.31.0

# Upgrade an existing package to the latest version
pip install --upgrade requests

# Uninstall a package
pip uninstall requests

# List all installed packages
pip list

# Show details about a specific installed package
pip show requests
Watch out

On some systems, pip refers to the Python 2 pip and pip3 refers to the Python 3 pip. If pip install installs to the wrong version, try pip3 install. You can also use python -m pip install to guarantee you are using pip for the Python version you intend.

Once pip installs a package, you import it in your Python file exactly the same way you import a standard library module — with the import statement. The package does not need any special setup beyond the installation step.

python
# After running: pip install requests

import requests

response = requests.get("https://api.github.com")
print(response.status_code)  # 200 means success

How to Install and Use a Python Package

Follow these steps any time you want to add a third-party package to a Python project.

  1. Confirm Python and pip are installed

    Open your terminal and run python --version and pip --version. Both commands should print a version number. If pip is missing, run python -m ensurepip --upgrade to install it.

  2. Find the package name on PyPI

    Visit pypi.org and search for the functionality you need, or look up the library in its own documentation. Note the exact package name — that is what you pass to pip.

  3. Install the package with pip

    In your terminal, run pip install package-name, replacing package-name with the name from PyPI. For example: pip install requests. pip downloads the package and any other packages it depends on.

  4. Import the package in your code

    At the top of your Python file, add import package_name or from package_name import something. Run your script. If no ModuleNotFoundError appears, the import succeeded.

  5. Verify the installation

    Run pip show package-name in the terminal. pip prints the installed version, the install location, and any package dependencies. This is useful for confirming exactly what was installed.

"pip is the package installer for Python. You can use pip to install packages from the Python Package Index." — pip documentation

Python Learning Summary Points

  1. A module is a single .py file. A package is a directory of modules that includes an __init__.py file. The word "library" is informal and usually refers to a collection of packages distributed together.
  2. The Python standard library ships with every Python installation. You import its modules directly without any additional installation step.
  3. Third-party packages live on PyPI and are installed using pip install package-name in your terminal. After installation, you import them the same way you import standard library modules.
  4. The import statement has two main forms: import module loads the whole module, while from module import name loads only the specific function or class you need.
  5. When Python raises a ModuleNotFoundError, the module is either not installed (install it with pip) or has a typo in the import statement.

Packages are the reason Python's ecosystem is so productive. Rather than writing everything from scratch, you stand on the work of thousands of other developers. Learning how to find, evaluate, and use packages well — starting with the standard library and expanding from there — dramatically expands what you can build.

check your understanding question 1 of 5

Frequently Asked Questions

A package in Python is a directory that contains one or more module files and a special __init__.py file. Packages let you organize related code into a named folder so it can be imported cleanly. The term is also used loosely to refer to any installable distribution you grab with pip.

A module is a single .py file that contains Python code. A package is a directory that holds multiple modules (and possibly sub-packages) along with an __init__.py file. All packages are made up of modules, but a single module is not a package.

You install a Python package using pip, the package installer that ships with Python. Run pip install package-name in your terminal. For example, pip install requests downloads and installs the requests library from PyPI.

pip is Python's standard package installer. It connects to the Python Package Index (PyPI), which hosts over 500,000 open-source packages, and downloads them to your machine. pip has been included automatically with Python since version 3.4.

The Python standard library is the collection of modules and packages that ship with every Python installation. It includes tools for file I/O, networking, math, date handling, JSON parsing, and much more. You do not need pip to use standard library modules — just import them directly.

__init__.py is a file inside a package directory that tells Python the directory should be treated as a package. It can be empty, or it can contain initialization code that runs when the package is first imported. In Python 3.3 and later, namespace packages can exist without __init__.py, but traditional packages still use it.

PyPI stands for the Python Package Index. It is the official online repository where developers publish open-source Python packages. When you run pip install, pip fetches the package from PyPI by default.

You import a package using the import statement. For example, import os imports the os module from the standard library. To import a specific function from a package, use from package import function_name. After installing a third-party package with pip, you import it the same way.

import module loads the entire module and requires you to prefix names with the module name, such as math.sqrt(). from module import name loads only the specific name you request, so you can call sqrt() directly. Both work; the choice affects how you reference items in your code.

No. Standard library modules like os, math, datetime, and json come bundled with every Python installation. You only need pip to install third-party packages that are not part of the standard library.