Back to articles list Articles
8 minutes read

Python Library Gems: Useful Python Packages

In this article, we will demonstrate some Python packages that aren’t very well-known but are very useful.

Packages are basically completed Python code (classes, functions, etc.) that you can use  in your projects. They are usually located in a specific directory of your environment. You can create your own custom Python packages or download plenty of fabulous and free Python packages from the PyPI official repository.   

If you’re not already familiar with Python, I suggest you check out our Learn Programming with Python track, which introduces you to the fundamentals of programming.

Before going further, it is essential to mention that the difference between a module, package, library, and framework in Python can be quite confusing. If you are interested in knowing the exact terminology, read our article on the difference between modules, packages, and libraries in Python. We’ll be using the terms package and library interchangeably in this article.

Ok, let’s begin our exploration of little-known but handy Python packages. First, we’ll review how to install them.

Installing Python Packages

To install a package from PyPI, you can use

pip
, the package installer for Python. It’s preinstalled in most modern distributions of Python and is very easy to use. For example, if you want to install a package named “awesome-package” in your environment, you can execute the following command in your terminal:

pip install awesome-package

By default, pip will install the last stable version of the package. If you want to install a specific version of the package (e.g. version 0.93), you can indicate it this way:

pip install awesome-package==0.93

Note that pip itself is a package; you can update it to the last version by executing the following command:

pip install --upgrade pip

You may wonder if you should install a package or write the code by yourself. The answer is: If you can save time and money, use the package! The enthusiastic Python community has already developed most of the calculations or algorithms you will need for your projects, so don’t waste your time. Use the Python packages!

You may have used (or at least heard about) NumPy, a Python package that allows working with big data structures. NumPy is the result of the colossal work of the Python community; it’s now an essential tool for data scientists. To learn more about NumPy, read our article An Introduction to NumPy.

Another popular and awesome library is Matplotlib, which allows you to create 2D graphs and plots that make data understandable. It is widely used in machine learning and data science; you can find out more by reading Python Drawing: An Introduction to Matplotlib elsewhere in our blog. And if you want to learn about Python’s other data science libraries, I recommend the article Top 15 Python Libraries for Data Science.

Now that we’ve gone over the basics of Python libraries, let’s talk about those underappreciated Python packages.

7 Useful Python Packages

In this section, I will give you the best little-known useful Python packages I’ve worked with. Some are very simple while others are more complex, but all are extremely powerful!

BeautifulSoup

BeautifulSoup is a Python library for extracting and parsing data from HTML and XML files.

Imagine that your company has stored all its blog articles in the HTML format within a relational database. The SEO department asks you to add the

nofollow
tag to all the articles’ external links. So, you decide to parse the articles with BeautifulSoup, find all the links (thanks to this library’s
find_all
function), add the
nofollow
tag for each link, and return the resulting string.

Here’s the code you’d use:

from bs4 import BeautifulSoup

# Get the article content and parse it as HTML
article_html = '<h1>Title</h1><p>text and text<a href="some_url">Click here</a></p>'
soup = BeautifulSoup(article_html, 'html.parser')

# Find all the links and add the nofollow tag
for link in soup.find_all('a'):
    link["rel"] = "nofollow"

print(str(soup))
# Shows: <h1>Title</h1><p>text and text<a href="some_url" rel="nofollow">Click here</a></p>

Pretty simple, right? You can check all the functions available in the BeautifulSoup documentation.

pyppeteer

The pyppeteer package is a Python version of Puppeteer, a Node.js library that provides a high-level API to control Chrome/Chromium functionalities. In simple words, it allows you to run a Chrome or Chromium browser inside your Python environment and automate simple actions like visiting a website, clicking on a button, etc.

Let’s say you are working in the e-commerce department of a sports equipment company. The Operations Team asks you to take screenshots of some products and keep them in PDF format. You decide to automate the process using the pyppeteer library:

import asyncio
from pyppeteer import launch

async def main():
	# Create a new browser instance
browser = await launch()
    	page = await browser.newPage()

	# Visit a specific URL
    	await page.goto("https://myshop.com/products?sku=1234")

	# Take a screenshot a keep it in a PDF file
  	await page.emulateMedia("screen")    
  	await page.pdf({"path": "exports/screen_1234.pdf"})    

	# Close the browser instance
  	await browser.close()

# Run the main function
asyncio.get_event_loop().run_until_complete(main())

Impressive! Note that pyppeteer needs the Asyncio library to perform the asynchronous calls. You can check the whole Pyppeteer documentation here.

Telethon

Telethon is a Python library that lets you interact with the Telegram API. You can send, receive, or wait for new Telegram messages inside your Python application. You can also send files to a contact or a group of contacts.

Let’s build on our previous example with pyppeteer. In order to completely automate the product's monitoring, you want to upload the PDF files to a Telegram group where the Operations team members are. You can do it very quickly with Telethon:

from telethon.sync import TelegramClient

with TelegramClient("name", api_id, api_hash) as client:
    channel = client.get_input_entity(channel_id)
    client.send_file(channel, "exports/screen_1234.pdf")
    client.run_until_disconnected()

Surprisingly simple! You only have to get a Telegram API ID and hash (which can be done in a few minutes) as specified in Telethon’s documentation.

PyWhatKit

Telegram lets you use its API freely. It’s not the same with WhatsApp: if you want to send messages to WhatsApp from your application, you will need WhatsApp Business. However, there is a cool hack for desktops – thanks to the PyWhatKit library. PyWhatKit is a Python library that works closely with the browser automation tool Selenium. When you want to send a message to WhatsApp, it opens WhatsApp Web in a new browser tab, puts the message in the input field, and automatically sends the message to your contact or group.

Imagine that you have created an emergency group in WhatsApp with your team members. You want to automatically forward some alerts you received from your laptop via e-mail (or some other source). You can do it very simply in this way:

import pywhatkit

pywhatkit.sendwhatmsg_to_group_instantly("X6jaeFeVWGQ5B9jWpSQFcz", "Alert: EC2 server reached 80% capacity")

You can easily find the group code in the invite screen of WhatsApp:

Python Standard Library Gems

It could not be easier! You can check all the fantastic functionalities of PyWhatKit in its official documentation.

XlsxWriter

XlsxWriter is a very powerful Python module for writing files in the Excel format. It supports adding text, numbers, formulas, images, and Excel macros – among other functionalities. XlsxWriter even integrates with pandas, the well-known Python package for data science.

Imagine that your boss asks you to generate a financial report for the current quarter. She needs it in Excel format to present it to the board. It could take hours to copy and paste the values from the database. You don’t want to do this task every quarter, so you decide to automate the process using XlsxWriter. This is a simple example of how you could do it:

import xlsxwriter

# Create a new XLSX file and a new tab
workbook = xlsxwriter.Workbook("Q4 Financial Report.xlsx")
worksheet = workbook.add_worksheet("October")

# Insert the company logo in the first cell
worksheet.insert_image("A1", "logo.png")

# Define a bold style for the column headers
header_format = workbook.add_format({"bold": True})

# Write some numbers, with row/column notation.
worksheet.write(3, 0, "Gross Margin", header_format)
worksheet.write(4, 0, 2.45)
worksheet.write(3, 0, "EBITDA", header_format)
worksheet.write(4, 0, 5.12)

# Save and close file
workbook.close()
Python Standard Library Gems

Result of the previous example

XlsxWriter is really impressive; here is the complete documentation.

Emoji

Have you ever noticed that some instant messaging apps will show a list of emojis if you type a colon followed by a keyword?

Python Standard Library Gems

Emojis shortcuts in WhatsApp for Desktop

You can use the Emoji package if you want to implement a similar logic in your Python app. Thanks to this small and friendly package, you can “emojize” plain text into emojis and “demojize” emojis into plain text. Here is a simple example:

import emoji

print(emoji.emojize('Learning :snake: is :fire:'))
# Shows: Learning ?? is ??

print(emoji.demojize('This emoji is a ??'))
# Shows: This emoji is a :dog_face:

You can find all the available functions of Emoji in the official documentation.

Faker

The Faker library generates fictionalized data – like names and addresses – in various languages.

Imagine that you want to test your in-development brand app with a database to ensure that it has sufficient capacity. The database contains the classic user data: name, address, job, company, birthday, etc. You can easily fill the tables with Faker, which provides good-looking and realistic data. Here’s an example:

from faker import Faker
from faker.providers import internet

fake = Faker("fr_FR")

# Generate a fake french name
print(fake.name())
# Shows: Robert Rousset

# Generate a fake french address
print(fake.address())
# Shows: 
# 99, boulevard Théodore Maurice
# 62856 Laporte-sur-Louis

# Generate a fake IPv4 address
fake.add_provider(internet)
print(fake.ipv4_private())
# Shows: 172.30.46.147

Faker provides much more awesome fake data; take a look at its documentation to learn more.

Time to Practice with Python Packages!

I hope this article made you want to discover more of the best Python packages. If you’re ready to start learning Python online, I highly recommend you take a look at our beginner-friendly Python Basics mini-track. It will give you solid foundations to begin your programming journey.

Thanks for reading, and keep it fun with Python!