Beginner Level
Intermediate Level
Advanced Level
Introduction
Python programming language provides a vast collection of modules and packages that are built-in or available for download from the internet. These modules and packages extend the functionality of the language and enable programmers to write efficient and complex programs with ease. One of the essential concepts of Python programming is packages. A package is a collection of modules and sub-packages that can be used to organize and structure code. In this tutorial, we will explore what packages are in Python, their purpose, and look at some code samples.
Table of Contents :
- What are Python Packages
- Advantages of Packages in Python
- Code sample for Python Package
- Subpackages
What are Python Packages :
- A package in Python is a way to organize related modules into a single parent directory(folder).
- A package is a collection of modules.
- Put simply, a package is a directory of Python modules.
- A package can contain one or more sub-packages, which can also contain other sub-packages.
- A sub-package is essentially a package within a package.
- Python packages are a way of structuring Python’s module namespace by using package directories.
- Packages are a way of grouping related modules together.
Advantages of Packages in Python
- Better organization : packages provide a way to organize modules into a hierarchical structure, making it easier to manage.
- Namespace separation : packages create a separate namespace for modules, preventing naming conflicts.
- Code re-usability : packages provide a way to reuse modules across different projects, reducing the need for rewriting code.
- Modularity : packages make it easier to break up large systems into manageable and modular components.
How to Create Packages in Python
- Creating a package is as simple as creating a directory with a
__init__.py
file inside of it. - Packages can be created by turning an ordinary directory into a package by just adding a special file called
__init__.py
to it. - The
__init__.py
file can be empty, or it can execute initialization code for the package. - The package directory should have a unique name that is unrelated to any module names.
- Modules can be placed directly in the package directory, or in sub-directories within the package directory.
- Example:
# my_package/__init__.py
from . import module1
from .sub_package import module2
__all__ = ["module1", "module2"]
Code sample for Python Package :
#my_package/__init__.py (folder name and __init__.py file)
def greeting(name):
print("Hello, " + name)
def farewell(name):
print("Goodbye, " + name)
# main.py
from my_package import greeting, farewell
greeting("John")
farewell("John")
Sub-packages
- Packages can contain sub-packages.
- Sub-packages are imported using the same syntax as regular packages.
__init__.py file of a python package
- The
__init__.py
file is executed when the package or module is imported. - It can be used to specify which modules are intended to be part of the package, using the
__all__
attribute. - It can also contain code to execute as part of the package initialization process.
API of a python package
- The
__init__.py
file can also be used to define the API of a package. - The API is defined by the variables and functions that are defined in the
__init__.py
file.
Data Files
- Packages can also contain data files.
- Data files are files that are used by a package, but are not part of the package’s API.
- Data files are typically located in the package’s data directory.
Importing a package in python
- To import a package in our code, we can use the
import
statement. - E.g. if we have a package named
mypackage
, we can import it like this :import mypackage
- This will make the
mypackage
package available in our code. - We can then access the functions, classes, and variables defined in
mypackage
. - When a package is imported, the Python interpreter searches for the package in the following order:
- The built-in modules.
- The site-packages folder.
- The standard Python library folder.
- Each directory in the Python path.
- When a package is imported, the
__init__.py
file is executed. - This file can contain code that initializes the package.
- To find out the name of a package, we can use the
__name__
attribute. - To find out the path of a package, we can use the
__path__
attribute.
Importing Modules from a Package :
- To import modules from a package, use the `import` statement followed by the package name and module name separated by a dot.
- To import all modules from a package, we can use the
from ... import *
statement. - To import a specific module from a package, we can use the
from ... import ...
statement. - Example:
# main.py
import my_package.module1
import my_package.sub_package.module2
my_package.module1.greeting("John")
my_package.sub_package.module2.farewell("John")
Importing Specific Function from the Module of a python Package :
- To import specific functions from a module within a package, use the `from` keyword followed by the package name, module name, and function name.
- Example:
# main.py
from my_package.module1 import greeting
greeting("John")
Installing Packages
- If you want to use a Python package that's not part of the standard library, you'll need to install it.
- The easiest way to install a package is with the
pip
tool. -
pip
is a package manager for Python. - It's used to install and manage packages from the Python Package Index (PyPI).
- To install a package with
pip
, we can use the following command :pip install package_name
- Where package_name with the name of the package we want to install.
Creating a Requirements File
- If we're working on a project that uses multiple Python packages, it's a good idea to create a requirements file.
- A requirements file is a text file that lists the packages our project needs to run.
- Creating a requirements file is simple.
- The
freeze
command is used to generate a list of installed packages :pip freeze > requirements.txt
- This will create a file named
requirements.txt
in the current directory. - The file will contain a list of all the packages installed on our system.
- We can then use the
pip install -r
command to install all the packages using the requirements file. - The syntax of the command is :
pip install -r requirements.txt
Reloading packages in python
- To reload a package, use the reload() function.
Prev. Tutorial : Creating modules
Next Tutorial : Creating packages