In Python programming, many tasks involve reusing the same blocks of code. Rewriting this code every time is inefficient. This is where the notion of packages comes in. Packages allow us to reuse code that was previously written very easily. Instead of writing the code again and again, we can simply import it when we want to use it. This saves time, and we can also write well-organized and cleaner programs. In this blog, we will talk about how to make a package in Python and how to import packages. We will also look at a few of the Python packages.
Table of Contents:
What is a Package in Python?
A folder on your computer contains different files and subfolders, all related to a specific topic or functionality. It may have all the files related to pictures, music, documents, etc. You can also create your folder. In Python, a package is similar to the folders on a computer. A package organizes related modules into a hierarchical structure, making large codebases more manageable and reusable. Packages help developers break down large codebases into smaller, reusable components. This modular approach enhances readability, maintainability, and promotes collaboration, testing, and reuse.
So, how does an interpreter know that it has to treat a package as a package and not a folder? By the existence of the __init__.py file. This file indicates to Python that the directory should be treated as a package. This was required in Python version 3.3 and before. From Python 3.3 onwards, implicit namespace packages are also supported, and the __init__.py file has become optional for such packages. Implicit Namespace packages are a mechanism for splitting a single Python package across multiple directories on disk. In traditional packages, though, the __init__.py file is still required.
For Example:
The pandas package in Python. This package contains sub-packages like core, io, plotting, util, and many more. Since this is an old package, it contains the __init__.py file. The presence of the __init__.py file indicates that this is a Python Package.
To Learn More about Packages and Advance Your Career with Python - Sign up now!
Expert-led. Project-based. Certificate included.
Key Components of a Python Package
To fully understand how packages work in Python, let us study the key components of a package. We will take the example of the Pandas package to fully understand the structure of a package. Pandas is a powerful, open-source tool for manipulating and analyzing data that is built on top of Python.
__init__.py File
This file serves as the foundation of any conventional Python package. It runs when you import the package or one of its modules. This file can be used to import specific functions or classes for convenience. It is also used to initialize variables that can be shared across the package modules and files. It can also be left empty.
Example:
Imagine you are building a calculator package with add.py, subtract.py, multiply.py, and other operations. Let us suppose you want to keep track of all the operations across the calculator. You will declare a variable named operations. Now, this variable has to be shared across various files. Therefore, you must declare a global variable, which can be done by declaring a variable in the __init__.py file.
Let us consider another example to understand the use of the __init__.py file. The pandas/__init__.py file helps make the library easier to use. Instead of forcing users to find and import tools from deep inside the library’s folders, it brings the most commonly used features to the top level.
Instead of coding something long where you will have to remember the module in which your function is, like:
from pandas.core.frame import DataFrame
Users can simply write:
import pandas as pd
df = pd.DataFrame()
This is possible because __init__.py includes lines that import important tools like DataFrame, Series, and read_csv, making them instantly available when you import pandas.
Modules
Modules are a collection of individual Python files within a package. They can be recognized by the .py file extension. Each module contains related classes, functions, or constants that are organized by an area of functionality.
Example:
The pandas package includes several modules, such as frame.py, series.py, parsers.py, etc.
Sub-Packages
A sub-package is a package within a package. It is a subfolder with its own __init__.py file inside it. Sub-packages are used to divide a large projects into a modular, scalable structure. Sub-packages can separate different functionalities, so you can import only the part of the package that you need. This saves space and memory.
Example:
Let us consider the pandas package.
- pandas.core: This package contains core data structures like DataFrame, Series, and Index, along with fundamental logic.
- pandas.io: It manages reading and writing data in CSV, Excel, and JSON formats.
- pandas.plotting: This sub-package offers fundamental visualization features for plotting various types of data.
- pandas.util: This sub-package provides helpful utility functions for internal use in the pandas package.
Using sub-packages is like dividing a big toolbox into smaller drawers. Each drawer has tools for a specific job. You don’t need to grab everything at once. You just take what you need.
The metadata files are a particularly valuable resource when distributing your package. Sharing your work with other Python developers is called distributing your package, and this is done through submitting your code to a platform like PyPI (Python Package Index). Once uploaded, anyone in the world can install your package with a simple pip install your-package-name, as they do with other popular packages such as pandas, numpy, or requests. There are metadata files of configuration, tools, and documentation to guide the third-party users on how to install, configure, and use your package.
Example:
The pandas include several standard metadata files like setup.py, pyproject.toml, requirements.txt, and readme.MD, LICENSE. These files ensure that pandas can be installed and maintained across different environments.
How to Use Packages in Python?
You have to import the package to use the modules of the package and its functionalities. There are several other ways in which key components of the package can be imported into your Python file. You can either import the whole package or a sub-package, or a function.
Importing a Package
It is important to import a package into your Python file for you to use it in your programs. When you import a Package, its top-level modules and sub-packages become accessible, but not all code is automatically loaded until explicitly used. This is due to Python’s lazy loading behavior.
Syntax:
import package_name
Importing Specific Modules or Functions
To keep the code clean and make it more readable, you import only what you will use from the package. This way, you won’t have to refer to the module name every time you use a function.
Syntax:
from package_name.module_name import function_name
Example:
Instead of:
import math
print(math.sqrt(25))
You should use:
from math import sqrt
print(sqrt(25))
Using Aliases with the as Keyword
It’s possible to give an alias to any Python package using the as keyword. This is very common with libraries like pandas or numpy, where abbreviations can help your code appear less cluttered.
Syntax:
import package_name as alias_name
Installing Third-Party Packages
If a package is not inside the standard library of Python, you will have to install it first using pip (Python’s package manager). Once all prerequisites are downloaded on your system, you will be able to import them into your file without raising any errors.
Syntax:
pip install package_name
Accessing Sub-packages and Sub-modules
In Python, packages can contain sub-packages like folders in folders. Each sub-package helps organize the related software, like in big libraries such as pandas. You use dot notation to call the modules and functions inside a sub-package. It is similar to fetching the search result after you enter the name in the search bar.
Syntax:
from package_name.subpackage_name import module_or_function
Get 100% Hike!
Master Most in Demand Skills Now!
How to Create Your Own Package in Python?
Creating your package in Python allows you to organize the code within your project, reuse functions, and share your code with others. This is just like pandas and numpy! You can think of a package as a directory with a lot of different Python files and modules.
Now, let us learn step by step how to create a package from scratch using a custom utility package named myutils.
Step 1: Create a Package Folder
Create a folder named myutils on your desktop (or laptop) that will be considered the root directory of your custom package. This folder will contain data that is related to the package, like metadata, sub-packages, modules, etc.
Step 2: Add the __init__.py File
In the myutils folder, you need to create a file called __init__. py which will tell the interpreter that this folder is a Python package and not a normal directory.
Step 3: Add your subpackages and Module
Now you have to create the Python files that you want to add to the package as a module. Save these files as .py, and the interpreter will consider them to be a module in the package. As you can see, we didn’t use a subpackage in this example, but if needed, you can add a subpackage to your package as well. Just create a new folder and add an __init__.py file in it.
Example:
Step 4: Distributing the Package
If you want to share your package with the other developers on PyPI, you must
- Add metadata files like setup.py, README.md, and LICENSE
- Build and upload your package using tools like setuptools and twine
Step 5: Using Your Package
Here we can use the newly created Python package in a program and see how it works. Let’s create a new file called main.py. This file should be outside the myutils package folder.
Note that the interpreter will raise a ModuleNotFoundError in case the module cannot be found. To avoid this error, make sure that the myutils folder is located in the same directory as main.py or that its path is set inside the PYTHONPATH environment variable.
Code:
Output:
Explanation: The code example uses the features of the custom math_utils and string_utils modules that are imported from the myutils package. It manipulates strings and carries out simple mathematical operations, showing the outcomes on the console.
Package Examples in Python
Python provides an extensive list of robust packages that are commonly used in diverse fields. Let’s examine a few well-known examples and easily grasp their essential roles.
For Web Framework
- Django: A package for implementing a web framework that uses the Model-View-Template (MVT) architecture to help Python developers create scalable web applications.
- Flask: A lightweight web framework that facilitates the rapid, easy, and flexible development of web applications.
For Artificial Intelligence and Machine Learning
- Deep Learning
- TensorFlow: An open-source framework for creating deep learning models, including neural networks.
- Keras: A user-friendly interface for building deep learning models that runs on top of TensorFlow, simplifying model creation and training.
- Data Visualization
- Matplotlib: This package allows you to plot graphs and visualize the data.
- Seaborn: Built on top of Matplotlib, it offers a high-level interface and attractive default styles for statistical plots.
- Natural Language Processing (NLP)
- NLTK (Natural Language Toolkit): NLTK is a toolkit that provides tools for working with human language data, enabling tasks like tokenization, parsing, and sentiment analysis.
- spaCy: A library for sophisticated natural language processing that provides user-friendly tools for named entity recognition, text analysis, and part-of-speech tagging.
- Computer Vision
- OpenCV: A package for computer vision tasks that need real-time processing for object identification.
- Pillow: A Python Imaging Library (PIL) fork offers simple tools for opening, modifying, and storing a wide variety of image formats.
Utility and Core Packages
- requests: A simple, yet powerful HTTP package for making requests to web services and APIs.
- numpy: A package for numerical computing in Python, offering support for large multi-dimensional arrays and matrices.
- pandas: A library for data manipulation and analysis, providing data structures like DataFrame to work with structured data.
- pytest: A testing framework that helps you write simple, scalable test cases for your Python code.
For Game Development
- Pygame: A library that simplifies writing video games by providing tools for graphics, sound, and event handling.
- pyKyra: A simple game engine for Python, enabling rapid development of 2D games with built-in support for sprites, collisions, and more.
These are just a few of the many powerful packages available in Python for various tasks. Depending on your project, choosing the right package can save you time and effort while improving the quality and scalability of your code.
Kickstart Your Coding Journey with Python – 100% Free
Learn the basics of Python through hands-on practice and projects.
Conclusion
Python packages are tools for sharing, reusing, and structuring code in Python. Packages help maintain, organize, and modularize codebases from simple applications to big data analysis to graphics development. As your project grows, you will want to learn to use and build your packages as well as start using the large Python library ecosystem. Learning how to build your own Python packages and how to use other existing packages makes your coding process more productive. Ensure you follow best practices in modern software development.
To take your skills to the next level, check out this Python training course and gain hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
Packages in Python – FAQs
Q1. What is a package in Python?
It is a folder with several modules and an __init__.py file instructing the Python interpreter to treat it as a package.
Q2. What are the top 10 Python libraries?
NumPy, Pandas, Matplotlib, Seaborn, Scikit-learn, TensorFlow, Keras, Flask, Requests, BeautifulSoup, these are widely used for data analysis, machine learning, web development, and automation.
Q3. How many types of packages are there in Python?
There are two types of packages: Regular package and namespace package.
Q4. What is the difference between a package and a module in Python?
A package is a folder containing multiple modules, while a module is a single .py file.
Q5. Is NumPy a module or a package?
NumPy is a package in Python.