How are Python Libraries different from Python Modules?

How are Python Libraries different from Python Modules?

 

[What is a Library / Module / Package in Python?]

In Python, a Library is a coherent set of Python modules while a Module is any set of statements that are contained in a Python (.py) file.

 

Added to the Python list of confusing terminologies is Python Packages. Python Packages are just Python Libraries but ones that are;

 – accessible via a common name, &
 – which can be installed from package managers (e.g. pypi).

Python Libraries including Packages do exist in the same directory, that is, on the Python search path.

Examples of Python Libraries include the Python Standard Library, while Python Modules include Functions, Classes, etc.

 

[How can you use Python Libraries/Modules of interests in a Python project?]

Libraries and modules are important in organizing code especially as it grows significantly. For instance, modules in the Python Standard Library, which ships with Python can be imported using the “import” keyword to use them. The syntax and semantics of the “import” keyword and all keywords also ship, or are predefined in Python Libraries/Modules which come preconfigured with Python, so that Python recognizes them as well as their use cases.

To illustrate a usecase of Libraries/Modules, we can use modules in the Python ‘pathology‘ package to get the location of the current script. In ‘pathology‘, the ‘pathlibPath class is predefined and in it, and we can access the module ‘script_dir’ which we will use to get the current path;

# Python Libraries/Packages && Modules

from pathology.Path import script_dir
print(open(str(script_dir()/file.py).read()))

# the above code snippet is a valid usecase

In Python, the ‘from‘ keyword is used when one wants to use (import) one of the modules.

 

[Examples of Python Libraries and Modules]

Python has a vast number of Libraries which contain an even larger number of modules, with each Library serving a certain purpose.

For example, the Python Standard Library contain built-in modules that provide different functionalities, like, data types, text/binary processing, functional programming, file/directory access, cryptographic services, operating system services, internet protocols and support, GUI with Tk, Importing modules, etc.

Other libraries are not as wide but are rather developed purposely for a specific reason, or to help solve problems of a certain nature, e.g. Scrapy library, PyGTK library, etc.! Below is a description of some of the most popular Python libraries, with an exception of the Python Standard Library;

1. Scrapy – a library for crawling and scraping the web, used in data mining.
2. SciPy – a scientific computations library that includes NumPy library in its core.
3. Pandas – a library for creating data structures, for data analysis and data science.
4. Matplotlib – a 2D plotting library for numerical ploting to visualize data, etc.
5. PyGTK – a library for creating Graphical User Interface (GUI) applications in Python.

 

How are Python Libraries different from Python Modules?
Programming | thetqweb