How Can We Help?
We use packages to develop an extensive application, including many modules. If the number of modules increases, it becomes difficult to keep track of them. So we need a structured organization of all modules with a grouping of similar modules, and in python, it is possible by Packages.
What are Packages?
Packages are a hierarchical file structure that defines a single Python application environment, which consists of modules and sub-modules, and so on. Packages use dot notation to import modules or sub-modules.
We place similar modules in one package and different modules in different packages. Using packaging, we can easily manage our large/complex program, which is easy to understand. A package must contain a file named “__init__.py“, which includes all the modules and sub-module details of the package by writing the initialization code of the package.
Let’s take an example of the Employee Management App package. We can organize the modules and sub-modules of this package as below.
In this package, Employee Details, Leave Management and Cafeteria Management are the Sub-packages/Sub-modules. Per the image below, the package includes individual files like Cafeteria Management includes, Snacks_Detail.py, and Payment_Status.py. All sub-package includes a separate “__init__.py” file.
Check This: In case you want to learn more about Python Modules like what it is and how to use them.
How to Create Package in Python
We can create a package in python by the below steps.
- 1) Create a package folder in the python directory or place the package folder in the python directory
- Create a .py files, which we need to place in the package folder
- Create a __init__.py file in the package folder with the initialization of all files.
We can import packages as same as importing modules by import statement. We can take as many functions in each file against our requirement. We can import any function of packages by using the import statement.
For example, to create a ‘Blogs’ package with files such as Technicalblog.py and Newsblog.py. We need to first create a “Blogs” folder on the desktop as below.
Then created two “.py” files on the desktop and placed them in the Blogs folder.
Code of “Technicalblog.py” file.
def python():
print('Python is a new generation language.')
print('Python is an easy language.')
def java():
print('Java is a base language to create other java languages.')
print('Any Java programmer easily learn AngularJ and NodeJs.')
Code of “Newsblog.py” file.
def news1():
print('New technologies are Data Science, ML, Deep Learning and AI.')
def news2():
print('Demand of python developer is increasing as 9% more than other developers.')
Create a “__init__.py” file in the Blogs folder as below.
from Technicalblog import python, java
from Newsblog import news1, news2
Place the Blogs folder at the python /Lib folder path and run both files and the “__init__.py” file of the Blogs folder.
Import Blogs package.
The “.pyc” file would be created like the modules when we import the package.
Using the code below, we can easily import all the functions of both sub-packages/files.
Input Code:
import Blogs
print('News:')
print(news1())
print(news2())
print('Technical:')
print(python())
print(java())
Output:
News:
New technologies are Data Science, ML, Deep Learning, and AI.
The demand for python developers is increasing by 9% more than other developers.
Technical:
Python is a new generation language.
Python is an easy language.
Java is a base language for creating another java language.
Any Java programmer quickly learns AngularJ and NodeJs.
In this tutorial, we have learned what is python packages and how to create and organize them.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.But remember to understand the concept very well, you need to practice more.
Hopefully, it was clear and concise.