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.
Test Your Knowledge
Check your understanding of this lesson with a short quiz.
