Python is an interesting programming language, It doesn't let you to code up everything from scratch. You are allowed use already built-in code
to work your way through various tasks. This built-in codes are organized as modules, libraries or packages. This article is dedicated to acquaint you with the knowledge needed to work with them.
First we'll look at each and understand what they are and how they are used.
A script is a python file that has python code written in it. A script can run and perform the functions that are written in it. If you open an editor and write code and save it as a python file, that is a script. You don't use functions and classes in scripts.
A module is a python file with written code in it and can be imported into a script. A module is simply a block of related code saved in a file with the extension .py. When imported, the functions in the module work with your code to produce the intended result. Modules can also be imported into other modules. Modules can involve the use of functions and scripts.
For example, let's define a function that says hello when called:
def hello(): print("Hi there!")
Now you can save the file as hello.py. If you want to call the function in script, you simply use the keyword import followed by the name of the file you just saved or the file you want to use in your code. That is, import hello. Then you can now call the function in your code in this script you are currently writing. ie
import hello # Now we can call the function hello()
The output of the code will be: Hi there!
A perfect example of a module would be the random module, which is perfect when handling logic that needs error.
Modules have numerous benefits into any Python code:
They improve the development process. Python modules help you focus on one small portion of a task rather than an entire problem. This simplifies the development process and makes it less prone to errors. Furthermore, modules are usually written in a way that minimizes interdependency. Thus, it’s more viable for a team of several programmers to work on the same application. The functionality you define in one module can be used in different parts of an application, minimizing duplicate code.
Separate namespaces. With Python modules, you can define separate namespaces to avoid collisions between identifiers in different parts of your application.
A package is a collection related modules that work together to provide a certain functionality. The modules are contain within a folder and can imported as well. So a package as the make suggests "packages" numerous modules in a folder, what makes it a package and not a normal folder is the inclusion of a file called the _init_.py file that tells python that it is a package. Packages allow the hierarchical structure of the module namespace. The same way we organize our files on a hard drive into folders and
sub-folders, we can organize our modules into packages and sub packages.
Examples of the most common packages
- NumPy, which is used widely by data scientists.
- Pandas, pandas is a Python package for fast and efficient processing of tabular data, time series, matrix data, etc.
- Pytest, Pytest contains modules that are essential when you want to test your code.
Python library contains a collection of related modules and packages. Sometimes this term is often used interchangeably with “Python package” because packages can
also contain modules and other packages (sub packages). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.
Mostly, developers create Python libraries to share reusable code with the community to eliminate the need for writing code from scratch. When another developer is working on the same kind of project, they use this libraries.
Some of these libraries are referred to as open source projects, meaning you as a developer can contribute to the building of such with your chunk of code, improving the functionalities of the library.
Examples of libraries:
Matplotlib library is a standard library for generating data visualizations in Python. It supports building basic two-dimensional graphs as well as more complex animated and interactive visualizations.
PyTorch is an open-source deep-learning library built by Facebook’s AI Research lab to implement advanced neural networks and cutting-edge research ideas in industry and academia.
Pygame provides developers with tons of convenient features and tools to make game development a more intuitive task.
Requests is a part of a large collection of libraries designed to make Python HTTP requests simpler.
Beautiful Soup is a very popular Python library for getting data from the web. The modules and packages inside this library help extract useful information from HTML and XML files.
Some packages mentioned above are also referred to as libraries, like NumPy and Pandas.
Depending on what your are working on or the areas of your interest, you can use the modules, packages or libraries.
Use the links provided in this article to check out the modules, packages or libraries. You can always also visit the python documentation more information.