Python package; Through this tutorial, i am going to show you what is the package in python programming and how to create and use these packages in python.
Package in Python
The package is created in Python so that the developer / programmer can manage and orgnize the Python code well. With this, if a developer / programmer works in this app in the future, then it makes it easier to understand the code. Because you use the package to manage Python code very well.
When creating large projects in Python, it is divided into many modules and packages.
If your application program grows larger in size with a lot of modules, you place similar modules in one package and different modules in different packages. This makes a project (program) easy to manage and conceptually clear.
What is package in python
In Python, the package is a directory. Within this directory, the collection of module files , subdirectories, and other files is done.
This is used to keep your code inefficient modules, well-organized form. Which can be used easily.
A package directory in python must contain a file named
__init__.py in order for Python to consider it as a package. This file can be left empty but you generally place the initialization code for that package in this file.
A Python package directory can contain subdirectories and files, a Python package may contain sub-packages and modules.
For example: As you have created a package for an e-commerce website, there will be many types of modules inside this package file such as shop module, product module, product category module, product image module, customer module, customer address module, a Payment module, etc. There will be many more modules like this. It’s just for you to understand
How to create packages in python
Now, how you can make a package in Python.
You can easily create a package in Python by following the steps given below:
- Create a new folder named C:\FirstApp.
- Inside FirstApp, create a subfolder with the name ‘mypackage’.
- Create an empty __init__.py file in the mypackage folder for initializing package
After that, you need to create modules greet.py and functions.py with following code:
Create greet.py module and place the following code into your module file:
def SayHello(name): print("Hello " + name) return
After that, create functions.py and update the following code into your module file:
def sum(x,y): return x+y def average(x,y): return (x+y)/2 def power(x,y): return x**y
You have created a package called mypackage . And inside this package have created the following init.py, greet.py, and functions.py files.
How to import packages in python
You can import modules from a package using the dot (.) operator in python.
Now, you have created a package named mypackage in Python. With this you will import the module and test the package created.
Now open your terminal or python IDLE,
After this you will call a function from the mypackage package named Power () function:
>>> from mypackage import functions >>> functions.power(4,2) 16
It is also possible to import specific functions from a module in the package:
>>> from mypackage.functions import sum >>> sum(50,20) 70
The package that was made. From this, you took the example of two functions of the functions.py module. Now you have an example that returns an average of function number two.
In this function, you will pass 2 numbers and this function will give us an average of those two numbers.
>>> from mypackage import functions >>> functions.average(15,45) 30