What Is a Package in Python?
A package in Python is a way to organize related code.
In simple terms, a package is usually a folder that contains Python modules and sometimes other package folders.
This helps keep code grouped in a clear structure instead of putting everything into one large file.
Beginners often use packages before they know the term. For example, when you write an import statement, you may already be using a module that belongs to a package.
import math
import json
print(math.sqrt(16))
print(json.dumps({"name": "Sam"}))
Output:
4.0
{"name": "Sam"}
A package is a way to organize Python code. Beginners often use packages before they fully understand them.
What a package means in Python
A package helps organize related Python code.
Here is the basic idea:
- A package is usually a folder
- That folder contains one or more modules
- A module is usually a single
.pyfile - Packages make larger programs easier to manage
- You often notice packages when using
import
For example:
mathis a module- A folder containing several related
.pyfiles can be a package
If you are not fully sure what a module is, see what is a module in Python or Python modules explained.
Package vs module
A package and a module are not the same thing.
- A module is usually one Python file
- A package is a collection of modules in a folder
- You import a module with code like
import module_name - You can import from a package with code like
import package.module
Example:
import json
import xml.etree.ElementTree
In this example:
jsonis imported directlyxml.etree.ElementTreeshows a name inside a larger package structure
This page is only defining the term. If you want the full beginner explanation of imports, read how import works in Python or how to import a module in Python.
Simple package structure
A package often looks like a folder containing multiple Python files.
Example structure:
my_project/
└── tools/
├── __init__.py
├── math_helpers.py
└── string_helpers.py
In this example:
toolsis the package foldermath_helpers.pyis a modulestring_helpers.pyis another module
Related code is grouped together inside one folder.
A package can also contain subpackages, which are package folders inside other package folders.
Example:
my_project/
└── tools/
├── __init__.py
├── math_helpers.py
└── text/
├── __init__.py
└── string_helpers.py
This makes it easier to organize larger projects.
Why packages are useful
Packages are useful because they help structure code clearly.
They help by:
- Keeping code organized
- Grouping related features together
- Reducing confusion in larger programs
- Making code easier to reuse
- Making code easier to share
Without packages, a project can quickly become messy if every feature is placed in one file.
Common examples beginners already use
Beginners often use packages and modules from Python’s standard library.
Common examples include:
jsonos
You may also install third-party tools with pip. Many of those are distributed as packages.
Important beginner note:
- You do not need to build your own package before you can use one
- You can use packages just by importing them
- You can install many external packages with
pip
If you want to do that next, see how to install a Python package with pip.
Important beginner note about __init__.py
Older explanations often say that a package must contain a file named __init__.py.
That file is still very common and still useful.
In modern Python, some packages can work without __init__.py.
For beginner learning, the important thing to know is:
- You will often see
__init__.pyin package folders - It is normal for package examples to include it
- Its presence often helps show that a folder is meant to be part of a package structure
So if you see this file, do not worry. It is a normal part of many Python package examples.
When this term is usually confusing
The word package is often confusing because beginners mix it up with other Python terms.
Common confusion points:
- Mixing up package, module, and library
- Thinking every imported name is a package
- Thinking
piponly installs modules - Confusing an ordinary folder with an importable package
A useful way to remember it:
- Module = usually one
.pyfile - Package = a folder that groups related modules
- Library = a broader term for reusable code
Common mistakes
These are common mistakes beginners make when learning this term:
- Confusing a single
.pyfile with a package - Thinking package and library mean exactly the same thing
- Not realizing that packages help organize multiple modules
- Seeing
import package.moduleand not knowing that it often matches a folder structure
If you run into import problems while working with packages, these commands can help:
python --version
python -c "import json; print(json.__file__)"
python -c "import os; print(os.__name__)"
python -c "import sys; print(sys.path)"
What these do:
python --versionshows which Python version you are usingimport json; print(json.__file__)shows where the imported code comes fromimport os; print(os.__name__)shows the imported module nameimport sys; print(sys.path)shows where Python looks for importable code
If Python cannot find a package or module, see ModuleNotFoundError: No module named x or ImportError: No module named x.
FAQ
Is a package the same as a module in Python?
No. A module is usually one Python file. A package is a folder that groups related modules.
Is every folder a Python package?
No. A folder becomes useful as a package when Python can import from it.
Do Python packages always need __init__.py?
Not always in modern Python, but beginners will often see that file in package examples.
What is the difference between a package and a library?
A package is a Python code organization unit. A library is a broader term for reusable code and may contain one or more packages.
Can I install packages with pip?
Yes. Many third-party tools installed with pip are distributed as packages.