Press "Enter" to skip to content

Python Notes 9 : Modules & Packages

A module is a file containing Python definitions and statements. A lot of modules gets installed on your system with the basic python installation. However user have the flexibility of exploring other modules online for specialized purposes. This section explains in details, how to add a module to your code & How to install a module from the internet.

Importing a module

  • import math
    • This is a built-in module in python. It contains a a lot of maths functions (e.g. sqrt()) that can be readily used by the programmer
  • To download a module in python, use command prompt & Enter the follwing
    • conda install “module name” OR
    • pip install “module name”
  • Use GitHub for exploring packages for python

Creating a module

Create firstModule.py then write the following function:

def helloworld():

print(“Hello this is a module”)

> import firstModule

firstModule.helloworld()

O/P -> Hello this is a module

To group many .py files put them in a folder. Any folder with an __init__.py is considered a module by python and you can call them a package

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *