All articles

10 Useful Python Libraries Few People Know About

10 Useful Python Libraries Few People Know About

Everyone knows requests, pandas and Django. But around them there are many small Python libraries that solve one specific task and do it very well: a nice terminal interface, debugging without a scattering of print calls, network traffic analysis, test data, spell checking.

This list contains libraries I've worked with myself. For each one: why you'd need it, a minimal example and the pitfalls worth knowing before you pull the dependency into a project.

Textual: apps with an interface right in the terminal

Textual is a framework for TUI applications built on top of Rich. It comes with ready-made widgets: buttons, checkboxes, dropdowns, data tables, radio buttons, a Markdown viewer, in-app search. Everything works from the keyboard, and you can build your own widgets from existing ones.

A minimal app for reading Markdown with a table of contents:

from pathlib import Path

from textual.app import App, ComposeResult
from textual.widgets import Footer, Header, MarkdownViewer


class ReaderApp(App):
    def compose(self) -> ComposeResult:
        yield Header()
        yield MarkdownViewer(Path("book.md").read_text(), show_table_of_contents=True)
        yield Footer()


if __name__ == "__main__":
    ReaderApp().run()

I wrote a text reader in Textual: the whole interface is just loading Markdown files, and theme switching and search come almost for free through the built-in command palette. Downsides I ran into with the Markdown widget: you can't select and copy text, there's no context menu, and a link to an anchor inside a chapter opens the chapter's beginning rather than the right spot. That's enough for viewing, but not yet for a full-fledged reader.

The textual-web project lets you open such an app in a browser. For example, run it on your home computer and read from an e-reader over the local network, without distracting apps.

IceCream: debugging instead of print

IceCream replaces debug print calls. The ic() function prints the expression, its value and the call site by itself:

from icecream import ic


def add(a: int, b: int) -> int:
    return a + b


ic(add(2, 3))
# ic| add(2, 3): 5

It's handy when you need to trace a chain of nested calls: sprinkle in ic() — and you immediately see what gets called and in what order. It doesn't replace a debugger or logging, but for quickly checking a hypothesis it's often more convenient than either.

PyShark: traffic analysis from Python

PyShark is a wrapper around tshark, the command-line part of Wireshark. It reads saved dumps and captures packets in real time:

import pyshark

capture = pyshark.LiveCapture(interface="eth0")
for packet in capture.sniff_continuously(packet_count=10):
    print(packet.highest_layer, packet.length)

For dumps you use pyshark.FileCapture("dump.pcap"). You can build your own mini-monitor on top of it: spot suspicious activity — send a Telegram notification or close a port. You need Wireshark/tshark installed and permission to capture traffic. And of course, you should only analyze your own network.

elevate: running with administrator rights

elevate restarts the current script with administrator rights on Windows, macOS and Linux in one line:

from elevate import elevate

elevate()

If your program supports a single platform, it's simpler to write the check yourself: on Windows it's a few lines with ctypes, on Linux it's checking os.geteuid() == 0. If you support several platforms, the library saves you from these details.

Flet and FastAPI

Flet is a Python GUI framework that builds the interface on Flutter. One codebase gives you desktop and web apps, with animations, lots of ready-made widgets and async support so the interface doesn't freeze during long operations.

FastAPI is one of the fastest Python web frameworks. Three things I like about it: very clear documentation, the choice between sync and async code, and automatic interactive API docs where you can test requests right from the browser.

Mimesis: fast test data

Mimesis generates fake data — names, addresses, emails, phone numbers, text — for filling forms, tests and demo databases. Different locales are supported, including Russian:

from mimesis import Person
from mimesis.locales import Locale

person = Person(Locale.EN)
print(person.full_name(), person.email(), person.telephone())

Every run produces new values. According to the authors' benchmarks, Mimesis is noticeably faster than Faker, which matters when you need millions of records.

Fake2db: a test database with one command

Fake2db creates a database already filled with test tables — handy for checking SQL queries at different volumes:

pip install fake2db
fake2db --rows 1000 --db sqlite

SQLite, MySQL, PostgreSQL, MongoDB, Redis and CouchDB are supported — each needs its own driver. The project hasn't been updated in a long time, so for serious work it's more reliable to write a Mimesis-based generator for your own schema.

translators and pyspellchecker

translators gives you one interface to dozens of online translators and can translate HTML without touching the tags:

import translators as ts

print(ts.translate_text("Привет, мир!", translator="alibaba", to_language="en"))
print(ts.translate_html("<p>Доброе утро</p>", translator="alibaba", to_language="en"))

Quality depends on the chosen engine, and the library itself calls the services' public web interfaces, so it can break when they change. For production, official APIs are a better choice.

pyspellchecker is spell checking without external services, including for Russian. It works like the suggestions on your phone: it finds unknown words and suggests corrections.

from spellchecker import SpellChecker

spell = SpellChecker(language="en")
print(spell.correction("progrem"))
print(spell.unknown(["hello", "progrem"]))  # {"progrem"}

You can add your own terms with spell.word_frequency.load_words([...]).

Which library to use for what

Task Library
A terminal interface Textual
Quick debugging IceCream
Traffic analysis PyShark
Administrator rights elevate
Desktop and web GUI Flet
APIs and web services FastAPI
Test data Mimesis, Fake2db
Translation and spelling translators, pyspellchecker

Before adding any of them to a project, check when the last release was and how many issues are open: small libraries sometimes get abandoned. And to keep the code around them tidy — with clear classes, interfaces and layers — my Python OOP course will come in handy. How not to eat all your memory with large collections of test data is covered in why a Python list can eat all your memory.

Sources