Packages and Modules in Python

We learned that Python functions and methods are really useful and can be used to solve many of our problems. We can even take functions written by others to solve our problems. When new functions and methods are written, they can be standardized and distributed so that a large numebr of Python developers can use them in their projects. However, it's not a good idea to distribute all these functions as a part of the core Python distribution. This is where packages come into play. A Python package is like a directory of Python scripts. Each script is a module. Modules in Python are simply Python files with the .py extension, which implement a set of functions. Each Module is designed to solve a particular set of problems.

Python is heavily dependent on itā€™s vast array of packages. The Python Package Index lists 90k+ packages available for download. Programmers can also create their own packages and make them available through Python Package Index from where others can download and use them.

Packages are used by importing the package at the beginning of your script, which allows you to then access

their specialized functions or objects. Below is a short list of the most widely used packages:

  • NumPy:Ā "the fundamental package for scientific computing with Python," the source of most linear algebra functions, data import functions, and general computation
  • SciPy:Ā builds on top of Numpy to include more specific, higher level functionality including image processing, statistics, and optimization
  • Matplotlib:Ā the primary way to generate figures in Python, replicates much of the functionality of plotting in MATLAB (hence the name)

Again, things are very google-able if youā€™re looking for something specific, thereā€™s almost certainly a python package somewhere that has it.

Using Inbuilt Modules

Python already has some very useful inbuilt modules. These modules are considered to be a part of Python's standard environment. An example of an inbuilt module is theĀ mathĀ module which contains many useful math functions. To use a module, we need to first import it. Following are some examples of theĀ mathĀ package.

1import math
2#Calculate square root of the number 25
3x = math.sqrt(25)
4print(x)
5#We can calculate the area of a circle using the formula Ļ€r^2
6#We can access the constant Ļ€ using math.pi
7r = 5
8Area = math.pi*5**2
9print(Area)
10

Apart fromĀ math, some other useful inbuilt modules are listed below:

  • sys - contains tools for system arguments, OS information etc.
  • os - for handling files, directories, executing external programs
  • re - for parsing regular expressions
  • datetime - for date and time conversions etc.
  • csv - for reading CSV tables
  • and many more

Different Ways of Importing

Depending on our needs, we can import a module in different ways.

import x

You can import a module by it's name likeĀ import math. When we import a module X in this manner, we need to use X.name to refer to an item called name that is defined in the module X.

1import math
2math.sqrt(25)
3math.pi
4

from X import *

In this case, you can directly refer to items in the module X, without using the ā€œX.ā€ prefix.

1from math import *
2sqrt(25)
3

from x import y

This is useful, if there is an object you specifically use frequently and would like to make it a part of your main namespace. So, only object y will be imported, nothing else.

1from math import sqrt
2print(sqrt(25))
3

Aliases for a package

If you have decided to access a module's objects from its own namespace, you can choose to alias the module with a name. For example the moduleĀ mathĀ can be given aliasĀ m.

1import math as m
2print(m.sqrt(25))
3

Installing Third-party Package

If a module is not inbuilt, it can be installed as a part of a third-part package.

We useĀ pipĀ a Python package management system to install and manage software packages written in Python. If you have installed Python 3 from python.org, you will already haveĀ pipĀ installed along with it.

1Installing PIP
2Visit http://pip.readthedocs.org/en/stable/installing/
3Download get-pip.py
4On terminal execute the command python get-pip.py
5

To install a package, you will use the commandĀ pip install <i>Package Name</i>. For example, you can install theĀ numpyĀ package using the following command.

pip install numpy

This will download and install theĀ numpyĀ package. Once the package is installed, you can import it in your Python program to start using it.