Python tutorials > Modules and Packages > Packages > What are packages?
What are packages?
In Python, a package is a way of organizing related modules into a directory hierarchy. It allows you to structure your code in a more organized and maintainable way. Think of it like a folder containing several Python files (modules) and a special file named __init__.py
(which can be empty in newer versions of Python). Packages help avoid naming conflicts and provide a logical namespace for your code.
Basic Package Structure
A package is essentially a directory containing: Example: In this example,
.py
files).__init__.py
file. This file is executed when the package is imported and can be used to initialize the package or define the package's contents. It can be empty, especially in Python 3.3 and later where implicit namespace packages are supported.
my_package/
__init__.py
module1.py
module2.py
my_package
is a package containing two modules, module1.py
and module2.py
.
Creating a Package
To create a package, simply create a directory and add an Example:__init__.py
file to it (even an empty one). Then, place your module files inside the directory.
my_package
.my_package
, create an empty file named __init__.py
.module1.py
and module2.py
, and place them inside my_package
.
Importing Modules from a Package
You can import modules from a package using different methods: The code snippet shows examples of these different import methods.
import package.module
: This imports the module as a submodule of the package. You access its members using package.module.member
.from package import module
: This imports the module directly into the current namespace. You access its members using module.member
.from package.module import member
: This imports a specific member (e.g., a function, class, or variable) from the module directly into the current namespace. You access the member directly.
import my_package.module1
my_package.module1.my_function()
from my_package import module2
module2.another_function()
from my_package.module1 import my_function
my_function()
The __init__.py
file
The The The code shows an example of how to use __init__.py
file serves several purposes:
__all__
variable is a list of strings that defines the public names of the package. When someone imports the package using from package import *
, only the names listed in __all__
will be imported. It helps to avoid importing unwanted modules or functions.__init__.py
to import modules and define the __all__
variable.
# my_package/__init__.py
from .module1 import my_function
from .module2 import another_function
__all__ = ['module1', 'module2', 'my_function', 'another_function']
Real-Life Use Case: Web Framework Structure
Many web frameworks use packages extensively. For example, a framework might have packages for: This modular structure makes the framework more organized, testable, and maintainable.
models
: Defines data models (e.g., database tables).views
: Handles user interface logic.controllers
: Manages the flow of data between models and views.utils
: Contains utility functions.
Best Practices
When to Use Packages
Use packages when:
Pros of Using Packages
Cons of Using Packages
FAQ
-
Do I always need an
__init__.py
file in a package?
In older versions of Python (before 3.3), you always needed an
__init__.py
file. In Python 3.3 and later, you can use implicit namespace packages, where the__init__.py
file is optional. However, it's generally recommended to include it, even if it's empty, for backwards compatibility and clarity. -
What is the difference between a module and a package?
A module is a single Python file (
.py
). A package is a directory that contains modules and an optional__init__.py
file. -
How do I handle circular imports within a package?
Circular imports can be tricky. Common solutions include: refactoring your code to reduce dependencies, using import statements within functions (local imports), or using deferred imports.