Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

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?

  1. Packages are a collection of modules.
  2. Packages are like a namespace.
  3. They are simply directories
  4. 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?

  1. PyPi is a repository for python
  2. PyPI is open source and its third party
  3. It is similar to NPM(node package manager)
  4. PyPI contains the packages which are common and required
  5. When you install the python, pip
  6. pip helps in downloading the packages from the command line

Module and package in python code snippet

One thought on “Module and package in python

Comments are closed.