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.

import math
#Calculate square root of the number 25
x = math.sqrt(25)
print(x)
#We can calculate the area of a circle using the formula πr^2
#We can access the constant π using math.pi
r = 5
Area = math.pi*5**2
print(Area)

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.

import math
math.sqrt(25)
math.pi

from X import *

In this case, you can directly refer to items in the module X, without using the “X.” prefix.

from math import *
sqrt(25)

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.

from math import sqrt
print(sqrt(25))

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.

import math as m
print(m.sqrt(25))

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.

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

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.

Please login to view this lesson.

With our free registration, you can access to all the lessons on finance, risk, data analytics and data science for finance professionals.

Sign in free