Module and package in python
In this chapter, you will learn about the module and packages in python. In the previous chapter, we have studied how the class can be created and how methods can be defined in the class.
What is a module in python?
Modules are .py scripts that contain a group of functions in it. Its good practice to group the related functions in the module, for example, while creating the user module, you can define the methods which are concerned with the user.
Now let’s write an example to create a user module and import it in our main app.
from user import module_details def init_app(): print('Initializing the app...') print('Calling the method module_details ') module_details() init_app() # output # Initializing the app... # Calling the method module_details # This is the user module...
Create a file with name user.py and paste the below code
def module_details(): print('This is the user module...')
What are packages in python?
- Packages are a collection of modules.
- Packages are like a namespace.
- They are simply directories
- When it comes to python packages, the PyPI is a resource for python packages. It’s similar to the NPM(node package manager) is a package manager for the JavaScript programming language.
What is PyPi?
- PyPi is a repository for python
- PyPI is open source and its third party
- It is similar to NPM(node package manager)
- PyPI contains the packages which are common and required
- When you install the python, pip
- pip helps in downloading the packages from the command line
One thought on “Module and package in python”
Comments are closed.