What is Python Package? In this tutorial, we will discuss what are Python Packages and how to use them in detail. So let us get started!
What are Packages?
On our computer, we have disks in which we can store files and that file could be anything videos, songs, word files, notepad files, pictures, etc. But instead of saving all the files altogether, we make folders for different files, for example, we save all the videos in the videos folder and all the songs inside the songs folder.
We create folders because with the help of folders we can gather a group of files having the same extension and it also helps us to access them easily. So, in python when we work on a big project, we create such folders or directories in which we store our modules (.py file), and those folders or directories in python are known as packages.
A package could have modules or sub-folders which are also known as sub-packages. And, every package or sub-package we create for modules should have an __init__.py file then only the folder or directory will be considered as a python package, and most of the time the _ _init__.py file is empty. Let’s understand it with an example:
Project1 (Package) __init__.py (module) main.py Project_1_0 (sub-package) __init.py Module_1_0_1.py Module_1_0_2.py Module_1_0_3.py Project_1_1(sub-package) __init.py Module_1_1_1.py Module_1_1_2.py Module_1_1_3.py Project_1_2(sub-package) __init.py Module_1_2_1.py Module_1_2_2.py Module_1_2_3.py
Importing module from packages and sub-packages:
With the help of the dot (.) operator, we can import different modules from packages.
Let’s understand it with an example:
import Project1.Project_1_0.Module1_0_1 # or you could also write it as from Project1.Project_1_0 import Module1_0_1
Python is rich with third-party Packages these are the packages that you can download from the internet with the help of the browser or you can just use python-pip install package_name in your command prompt it will install it. Some of the most famous 3 rd party packages are Numpy, orange, Django , Flask , etc.