Unlock the power of Python's libraries
Tags: python, machinelearning, webdev, programming
Published: 2022-06-03 21:05:03

*This post is from [Grokking Python](https://www.grokkingpython.com), a free newsletter available on Substack from Educative, the world’s best learning platform for software developers. It’s where we’ll share our team’s best Python-related info and resources — stuff we think could really enhance your understanding.*

Hey Grokking Python readers! 

We've been talking a lot in recent editions about **Python's real-world applications** for things like [web development](https://https://www.grokkingpython.com/p/python-in-action-web-development) and [machine learning](https://https://www.grokkingpython.com/p/python-in-action-machine-learning). Despite being distinct fields, they have one important thing in common: they both use Python's extensive assortment of **libraries and packages**.

These versatile and robust libraries and packages are among our favorite things about Python, so today we're going to look at them more closely. We'll start with the Python Standard Library, which you should definitely know about, then share five use cases that external libraries and packages serve to show just how diverse these offerings are. We'll also point you to some resources for getting hands-on with these tools. 

## First things first

Before diving in, we should get clear about some definitions. When we talk about **libraries and packages** in Python, we're basically just referring to collections of **modules**. (Packages can also contain sub-packages.) Modules are simply files containing Python code: functions or methods, classes, and variables. 

Libraries and packages save us time and help us do useful things because their code is predefined and contains reusable functions. And you don't have to look very hard to find this reusable code in action, since Python itself ships with a lot of it.
  
## The Python Standard Library

The **Python Standard Library** (PSL) is a set of modules distributed with Python. You can think of it as out-of-the-box Python, and it contains an extensive amount of functionality. A few examples of the types of modules in the PSL include:
* Built-in functions 
* High-level data types
* File and directory access
* Numeric and mathematical modules
* Data compression and archiving modules

![PSL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vt2hxucuwqi69aqpj2qw.jpg)

Among the useful PSL modules for Python beginners are `math`, `heapq`, and `random`: 
`math`: This module offers many mathematical functions like factorial and trigonometric operations.
`heapq`: This module lets you create the **heap** data structure, which can help with programming problems that involve finding the best element in a dataset.
`random`: This module allows you to generate different types of random numbers, which can come in handy in solving various coding problems. 

These modules barely scratch the surface of what the PSL offers. Take a look at the [PSL documentation's table of contents](https://docs.python.org/3/library/index.html), and you'll see just how extensive it is.

## 5 use cases and 30 libraries and packages

Although the PSL is robust, external libraries and packages make Python even more powerful. You can [search](https://pypi.org/search/) for these resources in the [Python Package Indexer](https://pypi.org) (PyPI), a repository of software for Python. 


![Python libraries](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/couit96t56x581ugavme.jpg)
 

PyPI contains libraries and packages for all sorts of use cases, five of which we'll discuss now.

### 1. Image processing

Python libraries can help with tasks like cropping photos, creating thumbnails, and grayscaling images. Some of these include: 
* [OpenCV](https://opencv.org)
* [SimpleCV](https://simplecv.org)
* [Pillow](https://pypi.org/project/Pillow/)
* [Mahotas](https://pypi.org/project/mahotas/)
* [Pgmagick](https://pypi.org/project/pgmagick/)

![SimpleCV](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7ci76n8cgh2ebnrxltf.jpg)

### 2. Graphical user interfaces

A graphical user interface (GUI) lets you interact with a computer using visual elements. GUIs make it easier to visualize your code and can be useful in all sorts of projects. Several Python libraries and tools make it easier to build GUIs:
* [Tkinter](https://docs.python.org/3/library/tkinter.html)
* [PyQt5](https://www.riverbankcomputing.com/software/pyqt/)
* [Kivy](https://kivy.org/#home)
* [wxPython](https://wxpython.org)
* [PySimpleGUI](https://pypi.org/project/PySimpleGUI/)
* [Pyforms](https://pypi.org/project/PyForms-GUI/)

![Kivy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08owmh478cd23oi3s9w5.jpg)
*Kivy's website includes a gallery of projects where it's used.* 

### 3. Web scraping

Web scraping automates the process of grabbing relevant data across multiple web pages, and Python is considered one of the best programming languages to use for it. You can use a number of tools and libraries to scrape the web:
* [Beautiful Soup](https://pypi.org/project/beautifulsoup4/)
* [MechanicalSoup](https://pypi.org/project/MechanicalSoup/)
* [Scrapy](https://scrapy.org)
* [Selenium](https://pypi.org/project/selenium/)
* [Requests](https://pypi.org/project/requests/)
* [LXML](https://pypi.org/project/lxml/)
* [Urllib](https://pypi.org/project/urllib3/)

![Scrapy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zigumxhhvwtkg638vyx.jpg)

### 4. Data science and machine learning

In the [previous edition of Grokking Python](https://www.grokkingpython.com/p/python-in-action-machine-learning), we covered some of the most commonly used libraries for these fields. To recap, this list included:
* [NumPy](https://numpy.org)
* [SciPy](https://scipy.org)
* [Pandas](https://pandas.pydata.org)
* [Matplotlib](https://matplotlib.org) 
* [Seaborn](https://seaborn.pydata.org)
* [TensorFlow](https://www.tensorflow.org) 
* [Scikit-learn](https://scikit-learn.org/stable/)

![MatplotLib](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lahwlesedbt3akk0wcer.jpg)

### 5. Game development

Yes, you can build games with Python, and the following resources will help you:
* [PyGame](https://www.pygame.org/news)
* [Pyglet](https://pyglet.org)
* [PyOpenGL](https://pypi.org/project/PyOpenGL/)
* [PyKira](https://pypi.org/project/pykira/)
* [Panda3D](https://www.panda3d.org)

![Python game development](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktoc7t7vr7qd0a81s235.jpg)

This is just a tiny sampling of the available external Python libraries and packages. Explore PyPI on your own, and see what you can discover. 

## Importing and installing libraries and packages

Before you can do anything with a Python library or package, you need to make sure you have the modules at your disposal. The process of doing so differs slightly for the PSL and external libraries, and we'll take a look at some basics next. (Code examples come from the Educative course [Learn Python 3 from Scratch](https://www.educative.io/courses/learn-python-3-from-scratch?eid=5082902844932096).)

### PSL

To use the methods of a module, you must **import the module** into your code. You can do this with the `import` keyword. Here's what that looks like when importing the `datetime` module, which contains methods for working with the current date and time:

```
import datetime

date_today = datetime.date.today()  # Current date
print(date_today)

time_today = datetime.datetime.now()
print(time_today.strftime("%H:%M:%S"))  # Current time
```

In that code, `datetime.date` and `datetime.datetime` are classes in the `datetime` module. Each class contains its own methods, which you access with the . operator. 

To import only a particular class from a module, you can use the `from` keyword:

```
from datetime import date

# Now we only have the methods of the date class
date_today = date.today()  # Current date
print(date_today)

# These won't work
# time_today = datetime.datetime.now()
# print (time_today.strftime("%H:%M:%S"))# Current time
```

Finally, you can give your own names to modules you import by using the as keyword. For example, the following code renames `datetime` to `dt`:

```
import datetime as dt

date_today = dt.date.today()  # Current date
print(date_today)

time_today = dt.datetime.now()
print(time_today.strftime("%H:%M:%S"))  # Current time
```
### External libraries

Importing modules from an external package works the same as with the PSL. The difference is that you must **install the package** first. You can find detailed instructions on installing these packages in the [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/installing-packages/).

## Projects and courses on Educative 

Hopefully, today's look at libraries and packages in Python has inspired you to try some out for yourself. To help you get started, we've collected some projects and courses that will teach you about notable libraries and packages:

* [Project: Visualize Stock Market Data with Python](https://www.educative.io/collection/page/10370001/5169748918140928/4859536187326464/project?eid=5082902844932096). This project uses Matplotlib and Pandas.
* [Project: Develop a Chatbot](https://www.educative.io/collection/page/10370001/6669435808514048/4557933108527104/project?eid=5082902844932096). This project involves the Natural Language Toolkit (NLTK) and TensorFlow, a machine learning platform.
* [Course: Simplifying Machine Learning with PyCaret](https://www.educative.io/courses/simplify-machine-learning-pycaret-python?eid=5082902844932096). PyCaret makes machine learning a lot more approachable.
* [Course: Deep Learning with PyTorch Step-by-Step](https://www.educative.io/courses/deep-learning-pytorch-fundamentals?eid=5082902844932096). PyTorch is another popular machine learning framework.
* [Course: Predictive Data Analysis With Python](https://www.educative.io/courses/predictive-data-analysis-with-python?eid=5082902844932096). This course teaches you about NumPy, Pandas, and Selenium, among other things.
* [Python Tkinter Tutorial: Build a Number Guessing Game](https://www.educative.io/blog/python-tkinter-tutorial-number-game?eid=5082902844932096). This article on the Educative blog links to four more tutorials for learning the GUI library Tkinter.

The web is full of other tutorials and hands-on learning opportunities for Python libraries and packages. Do you have a favorite library or tutorial that you'd like to share? Let us know in the comments. 

*As always, happy learning!*

### Start a discussion

Which Python library is your favorite? Was this article helpful? Let us know in the comments below!