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:
Again, things are very google-able if you’re looking for something specific, there’s almost certainly a python package somewhere that has it.
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
6Apart from math, some other useful inbuilt modules are listed below:
Depending on our needs, we can import a module in different ways.
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
4In this case, you can directly refer to items in the module X, without using the “X.” prefix.
1from math import *
2sqrt(25)
3This 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))
3If 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))
3If 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 getTo 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.