In this section, we are going to discuss the Python Libraries their function and some features of these libraries. But, before discussing Python Libraries, firstly we must have a better understanding of the terms like “Modules” and “Packages”.
A Module is a file which contains some Python code whereas the term Packages state that it is a directory of sub-packages and modules. After having a better understanding of these two we shift our discussion to the Python Libraries. So, A Library is a collection of files (known as Modules) that contains functions for use by other programs. It means that Python Libraries are a reusable piece of code that we can add to our programmes.
They may also contain data values like numerical constants and other things. Library’s contents are supposed to be related, but there’s no way to enforce that. The Python standard library is an extensive suite of modules that comes with Python itself. Many additional libraries are available from PyPI (the Python Package Index) and from here we can conclude that a package is a library that can be installed using a package manager like RubyGems or npm.
Writing Python Libraries
As we know that a Python library is a coherent collection of Python modules that is organised as a Python package. In general, it means that all modules live under the same directory and that directory is on the Python search path. A Python library is simply a .py file which is located on the path when we do the installation of our Python versions and whenever we think that we have to use it, we call its filename with no extension to import it. When we do so, all of the code is evaluated mostly if we would run the program as a script.
Let’s see How to implement these things in actual way in different steps:
Step 1: Here we write the classes and methods in a python file.
Step 2: We have to be attentive about the statement if_name_= ‘_main_’ that it must be written at end of every such python files, the feature of this statement in python is useful because it allows every .py file to serve as both a script and as a library as well.
Now see how to use Library:
- Import the library which has classes and methods to be used.
- Use the library file name to access the classes of the library.
- Have both the files in same directory or environment variable PYTHONPATH set to library file path if its in different directory.
Let’s create a library for operation to do operation backup, restore and update:
We have Library name as Operationlib and library file name as Operationlib.py
Backup( )-To take the backup of data
Restore( )- To restore the software to previous version
Update( )- To update the software to current versions
And this the actual way library will be used-
Implementation of Library:
We will write code of library in a python file.All the classes and methods of library will be in this file and these classes and methods will be reused by developer for their application. At the end of the file we will have if statement if__name__= ‘main’ The statement under this condition will be executed when this file is executed standalone as a script, otherwise this file is used as module.
Now the library Operationlib is available which can be used in user file Operationlib.py.
We have the classes and methods of library and we want to use them. The developer will reuse them using library file name.
Now we can place the library file and user file in the same directory, then we can run the user file. If we want to have the library file in a different directory. Then we have to set the library file path to environment variable PYTHONPATH. Then we can run the user file. In, this case any other user file from other directories can also use the library file.
The Output of above file is
Restoring to the previous version
Updating to the latest version
Some Important python libraries,their functions and features:
- Matplotlib: It is one of very important Python Library which helps us to deal with data analysing and is a numerical plotting library. Actually It is a Python 2D plotting library which produces quality figures in a variety of Hardcopy Formats and interactive environment across platforms. Matplotlib can be used in Python Scripts, Python and Python shell. It tries to make easy things easy and hard things possible. In this, we can generate plots, Histograms, Power Spectra etc with the few lines of codes.
- NumPy: It is one of the fundamental Library of Python, which has advanced math functions and a package of scientific computing with Python. It is useful for linear Algebra, Fourier Transformation and other various complex Mathematical functions.NumPy can also be used as an efficient multi-dimensional container of generic data, arbitrary data types are also defined in NumPy which makes it speedily integrate with a wide variety of database.
- Pandas: It is a library in Python which is used for Data Science. It is used for Data Analysis purpose as it provides fast, expressive and flexible data structures to easily work with structured and time-series data in Python Programming Language.
- Scipy: This library of Python is most popular, as we have been reading so much about this. It is just another form which may be used in place of NumPy. They use NumPy for more mathematical functions. SciPy uses NumPy arrays as their basic data structure and comes with modules for various commonly used task in scientific Programming, including Ordinary differential equations solving and signal processing.
- PyGame: It is a set of Python modules which is used to create video games. It consists of computer graphics and sound libraries that are designed to be used with the Python programming language. Pygame was officially written by Pete Shinners to replace PySDL. Pygame is suitable to create client-side applications that can be potentially wrapped in a standalone executable.
- Pyglet: It is a cross-platform windowing and multimedia library for python. Pyglet is an excellent choice for an object-oriented programming interface in developing games. In fact, it also finds use in developing other visually-rich applications for Mac OS X, Windows, and Linux. In the 90s, when people were bored, they resorted to playing Minecraft on their computers. Pyglet is the engine behind Minecraft.
- Scrapy: If your motive is fast, high-level screen scraping and web crawling, then go for Scrapy. It is used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing.
- SymPy: It is an open-source library for symbolic mathematics. With very simple and comprehensible code that is easily extensible, SymPy is a full-fledged Computer Algebra System (CAS). It is written in Python and hence does not need external libraries.
- Fabric: Along with being a library, Fabric is a command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating our entire workflow. With Fabric, we can execute local or remote shell commands, upload/download files, and even prompt running user for input, or abort execution.
- Pillow: It is a Python Imaging Library(PIL), which adds support for opening, manipulating, and saving images. The current version identifies and reads a large number of formats. Pillow allows us to get some basic information about images.
After reading this article now we know which libraries to go for if we choose to extend our career in Python. Many of the libraries help us with data-science as well. And if someone wishes to go out of his way, then create his own library, and get it published with the PyPI and help the community grow.
To read more about Python, click here.
By Mayank Mishra