In this post, you will learn how to leverage Python code that has already been written. This reusable code is available in the form of so-called modules and libraries. In what follows, you will learn about modules and libraries, as well as the differences between them. Also, this post contains useful information about the basics of commonly-used libraries and modules. Learning these fundamentals will be helpful later, when concepts are combined to accomplish a task in the field of Data Analytics.
What is a module?
In Python, a module is reusable code that you can build upon in a Python program. A module takes the form of a Python file, which is indicated by a .py file extension. A module typically consists of functions that have specific purposes.
If there is more than one Python file in a folder, it is possible to use a function from one Python file in a different Python file. For example, suppose there are two Python files, statistical.py and main.py, in the same folder. Perhaps the statistical.py file contains a useful function called average that outputs the average of a list of numbers called numbers. The programmer would like to use this averaging function in main.py, without rewriting the entire function. This can be accomplished with the keyword import. If
is written at the top of the main.py file, then the function average can be accessed and used in main.py, even though the function is implemented in a different file! This can be done by writing the following line of code in main.py.
In this example, a function called average from the module statistical.py was used in a different Python file called main.py. The Python language has several, built-in modules that are available for use. Two examples of these built-in modules are math and datetime. You can also create your own module by writing a python file.
What is a library?
Just like a module, a library is reusable code. The main difference between a library and a module is that a library can consist of related modules, but modules do not consist of related libraries. If a library is called name_of_library, the way to incorporate this library into your code is
An example of a library is NumPy, which is useful for scientific computing and machine learning. NumPy is also referred to as a package, so the terms package and library are similar.
Installing Libraries with conda
Before you can import a library, you need to install it so that the software is available to you. This is where conda becomes very useful—the installation of a library can be easily done with conda. Conda can also be used to install a module, assuming you didn’t write the module yourself. If you wrote the module yourself, the code is already available, so you don’t need to install that module from a location on the internet. For example, if you would like to install the library called NumPy with conda, you would type
conda install numpy
in your computer’s terminal. To keep track of all the modules and libraries that you have, you can focus on your environment as described in Chapter 2. After the required software is installed, you can import the software, at the top of a notebook in Jupyter Notebook. Once the import statement is there, you can access and utilize useful functions from that module or library.
pip documentation v23.1.2. https://pip.pypa.io/en/stable/
NumPy documentation — NumPy v1.24 Manual. https://numpy.org/doc/stable/#