Common Python 3 Third‑Party Libraries by Category with Code Examples
This article presents a comprehensive overview of widely used Python 3 third‑party libraries across multiple domains—including text processing, web development, databases, data analysis, computer vision, automation testing, security, and other utilities—offering concise descriptions and ready‑to‑run code snippets for each library.
Python is a powerful and versatile programming language with a rich ecosystem of third‑party libraries. This article lists common Python 3 libraries grouped by application domain, providing brief introductions and example code for each library.
1. Text Processing
Python offers several libraries for natural language processing and text analysis, such as NLTK, spaCy, TextBlob, Gensim, and PyPDF2.
NLTK example:
import nltk
# 下载必要的数据
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
# 文本分词
text = "Hello, how are you?"
tokens = nltk.word_tokenize(text)
print(tokens)
# 词性标注
tagged = nltk.pos_tag(tokens)
print(tagged)spaCy example:
import spacy
# 加载英文模型
nlp = spacy.load('en_core_web_sm')
# 分词
text = "Hello, how are you?"
doc = nlp(text)
tokens = [token.text for token in doc]
print(tokens)
# 命名实体识别
entities = [(entity.text, entity.label_) for entity in doc.ents]
print(entities)TextBlob example:
from textblob import TextBlob
# 情感分析
txt = "I love this library!"
blob = TextBlob(txt)
sentiment = blob.sentiment
print(sentiment.polarity)
# 文本翻译
txt = "Hello, how are you?"
blob = TextBlob(txt)
translated = blob.translate(to='fr')
print(translated)Gensim example:
from gensim import corpora, models
# 文本语料
documents = ["I love this library!", "This library has great features.", "I use it every day."]
# 创建词袋模型
texts = [[word for word in document.lower().split()] for document in documents]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
# 主题建模
lda_model = models.LdaModel(corpus, num_topics=2, id2word=dictionary, passes=10)
topics = lda_model.print_topics(num_topics=2)
for topic in topics:
print(topic)
# 文本相似性计算
query = "I like using this library."
query_bow = dictionary.doc2bow(query.lower().split())
query_vector = lda_model[query_bow]
similar_docs = sorted(lda_model[corpus], key=lambda x: abs(x[0][1] - query_vector[0][1]))
print(similar_docs)PyPDF2 example:
import PyPDF2
# 打开PDF文件
with open('example.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 提取文本
text = ''
for page in reader.pages:
text += page.extract_text()
print(text)2. Network and Web Development
Libraries such as requests, Flask, Django, Scrapy, and Tornado simplify HTTP communication, web application building, and web crawling.
requests example:
import requests
# 发送GET请求
response = requests.get('https://api.example.com/users')
data = response.json()
print(data)
# 发送POST请求
payload = {'username': 'john', 'password': 'secret'}
response = requests.post('https://api.example.com/login', data=payload)
data = response.json()
print(data)Flask example:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()Django example:
from django.http import HttpResponse
from django.urls import path
from django.views import View
class HelloView(View):
def get(self, request):
return HttpResponse('Hello, World!')
urlpatterns = [
path('', HelloView.as_view()),
]Scrapy example:
import scrapy
class MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://example.com']
def parse(self, response):
title = response.css('h1::text').get()
yield {'title': title}
# 运行爬虫
from scrapy.crawler import CrawlerProcess
process = CrawlerProcess()
process.crawl(MySpider)
process.start()Tornado example:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello, World!')
def make_app():
return tornado.web.Application([
(r'/', MainHandler),
])
if __name__ == '__main__':
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()3. Databases and Data Storage
SQLAlchemy, psycopg2, pymongo, redis‑py, and sqlite3 cover relational and NoSQL data access.
SQLAlchemy example:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# 创建数据库引擎
engine = create_engine('sqlite:///example.db', echo=True)
# 创建数据模型
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
# 创建数据表
Base.metadata.create_all(engine)
# 创建会话
Session = sessionmaker(bind=engine)
session = Session()
# 执行查询
users = session.query(User).all()
for user in users:
print(user.name, user.email)psycopg2 example:
import psycopg2
# 连接数据库
conn = psycopg2.connect(database="mydb", user="myuser", password="mypassword", host="localhost", port="5432")
# 执行查询
cur = conn.cursor()
cur.execute("SELECT * FROM users")
rows = cur.fetchall()
for row in rows:
print(row)
# 关闭连接
cur.close()
conn.close()pymongo example:
from pymongo import MongoClient
# 连接数据库
client = MongoClient('mongodb://localhost:27017/')
# 获取数据库和集合
db = client['mydb']
collection = db['users']
# 执行查询
users = collection.find()
for user in users:
print(user)
# 关闭连接
client.close()redis‑py example:
import redis
# 连接数据库
r = redis.Redis(host='localhost', port=6379, db=0)
# 设置键值对
r.set('mykey', 'myvalue')
# 获取数据
value = r.get('mykey')
print(value)
# 关闭连接
r.close()sqlite3 example:
import sqlite3
# 连接数据库
conn = sqlite3.connect('example.db')
# 执行查询
cur = conn.cursor()
cur.execute("SELECT * FROM users")
rows = cur.fetchall()
for row in rows:
print(row)
# 关闭连接
cur.close()
conn.close()4. Data Analysis and Scientific Computing
NumPy, pandas, Matplotlib, SciPy, and scikit‑learn support numerical computation, data manipulation, visualization, and machine learning.
NumPy example:
import numpy as np
# 创建数组
arr = np.array([1, 2, 3, 4, 5])
# 计算数组的平均值
mean = np.mean(arr)
print(mean)
# 计算数组的标准差
std = np.std(arr)
print(std)pandas example:
import pandas as pd
# 读取CSV文件
data = pd.read_csv('data.csv')
print(data.head())
# 计算列的统计信息
mean = data['column'].mean()
std = data['column'].std()
print(mean, std)Matplotlib example:
import matplotlib.pyplot as plt
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制折线图
plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()SciPy example (solving linear equations):
import numpy as np
from scipy.linalg import solve
# 定义系数矩阵和常数向量
A = np.array([[2, 3], [4, 5]])
b = np.array([5, 6])
# 解线性方程组
x = solve(A, b)
print(x)scikit‑learn example (linear regression):
from sklearn.linear_model import LinearRegression
# 创建数据
X = [[1], [2], [3], [4], [5]]
y = [2, 4, 6, 8, 10]
# 创建线性回归模型
model = LinearRegression()
# 训练模型
model.fit(X, y)
# 预测值
y_pred = model.predict([[6]])
print(y_pred)5. Image Processing and Computer Vision
OpenCV, Pillow, scikit‑image, PyTorch, and TensorFlow enable image manipulation, computer‑vision algorithms, and deep‑learning based vision tasks.
OpenCV example:
import cv2
# 读取图像
image = cv2.imread('image.jpg')
# 显示图像
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()Pillow example:
from PIL import Image
# 打开图像
image = Image.open('image.jpg')
# 调整图像大小
image_resized = image.resize((400, 300))
# 保存图像
image_resized.save('image_resized.jpg')scikit‑image example (edge detection):
import skimage
from skimage import data
from skimage.filters import sobel
import matplotlib.pyplot as plt
# 读取图像
image = data.camera()
# 边缘检测
edges = sobel(image)
# 显示图像
fig, ax = plt.subplots()
ax.imshow(edges, cmap='gray')
plt.show()PyTorch example (image classification inference):
import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
# 加载图像分类模型
model = models.resnet18(pretrained=True)
model.eval()
# 图像预处理
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# 加载图像
image = Image.open('image.jpg')
image_tensor = transform(image)
image_tensor = torch.unsqueeze(image_tensor, 0)
# 预测图像分类
output = model(image_tensor)
_, predicted_idx = torch.max(output, 1)
print(predicted_idx.item())TensorFlow example (ResNet‑50 inference):
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
# 加载图像分类模型
model = ResNet50(weights='imagenet')
# 加载图像
img_path = 'image.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# 预测图像分类
preds = model.predict(x)
decoded_preds = decode_predictions(preds, top=3)[0]
for _, label, prob in decoded_preds:
print(label, prob)6. Automation and Testing
Selenium, Pytest, unittest, Mock, and Coverage help automate web testing, write unit tests, mock dependencies, and measure code coverage.
Selenium example:
from selenium import webdriver
# 创建浏览器实例
driver = webdriver.Chrome()
# 打开网页
driver.get('https://www.example.com')
# 输入文本
search_input = driver.find_element_by_name('q')
search_input.send_keys('Hello World')
# 点击按钮
search_button = driver.find_element_by_css_selector('button[type="submit"]')
search_button.click()
# 关闭浏览器
driver.quit()Pytest example:
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
result = add(10, -5)
assert result == 5
assert add(0, 0) == 0unittest example:
import unittest
def add(a, b):
return a + b
class AddTestCase(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
self.assertEqual(add(10, -5), 5)
self.assertEqual(add(0, 0), 0)
if __name__ == '__main__':
unittest.main()Mock example:
from unittest.mock import Mock
def calculate_total(price, quantity):
return price * quantity
# 创建Mock对象
mock_function = Mock()
# 模拟函数调用
mock_function.return_value = 10
# 使用Mock对象进行测试
result = calculate_total(mock_function(5), 2)
mock_function.assert_called_once_with(5)
assert result == 20Coverage example:
$ coverage run test.py
$ coverage report -m7. Security and Encryption
cryptography, bcrypt, PyJWT, oauthlib, and PyCryptodome provide tools for encryption, password hashing, token handling, OAuth flows, and low‑level cryptographic operations.
cryptography example (Fernet symmetric encryption):
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 创建加密器
cipher = Fernet(key)
# 加密数据
data = b"Hello World"
encrypted_data = cipher.encrypt(data)
# 解密数据
decrypted_data = cipher.decrypt(encrypted_data)
print(decrypted_data)bcrypt example (password hashing):
import bcrypt
# 生成密码哈希
password = b"password123"
hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())
# 验证密码哈希
if bcrypt.checkpw(password, hashed_password):
print("密码正确")
else:
print("密码错误")PyJWT example (JSON Web Token):
import jwt
# 生成令牌
payload = {"user_id": 1234, "username": "john.doe"}
secret_key = "secret_key"
token = jwt.encode(payload, secret_key, algorithm="HS256")
# 验证令牌
decoded_token = jwt.decode(token, secret_key, algorithms=["HS256"])
print(decoded_token)oauthlib example (OAuth2 client credentials flow):
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
# 创建OAuth2会话
client_id = "your_client_id"
client_secret = "your_client_secret"
token_url = "https://api.example.com/oauth/token"
scope = ["read", "write"]
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
# 获取访问令牌
token = oauth.fetch_token(token_url=token_url, client_id=client_id, client_secret=client_secret, scope=scope)
print(token)PyCryptodome example (AES encryption):
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
# 生成密钥
key = get_random_bytes(16)
# 创建加密器
cipher = AES.new(key, AES.MODE_EAX)
# 加密数据
data = b"Hello World"
ciphertext, tag = cipher.encrypt_and_digest(data)
# 解密数据
cipher_decrypt = AES.new(key, AES.MODE_EAX, cipher.nonce)
decrypted_data = cipher_decrypt.decrypt_and_verify(ciphertext, tag)
print(decrypted_data)8. Other Common Libraries
datetime, logging, argparse, configparser, and asyncio are essential utilities for handling dates, logging, command‑line parsing, configuration files, and asynchronous programming.
datetime example:
import datetime
# 获取当前日期和时间
current_date = datetime.datetime.now()
# 格式化输出
formatted_date = current_date.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)logging example:
import logging
# 配置日志记录
logging.basicConfig(filename="app.log", level=logging.DEBUG)
# 记录日志
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")argparse example:
import argparse
# 创建解析器
parser = argparse.ArgumentParser(description="Process some integers.")
# 添加参数
parser.add_argument("integers", metavar="N", type=int, nargs="+", help="an integer for the accumulator")
parser.add_argument("--sum", dest="accumulate", action="store_const", const=sum, default=max, help="sum the integers (default: find the max)")
# 解析命令行参数
args = parser.parse_args()
# 执行操作
result = args.accumulate(args.integers)
print(result)configparser example:
import configparser
# 创建配置解析器
config = configparser.ConfigParser()
# 读取配置文件
config.read("config.ini")
# 获取配置值
username = config.get("Credentials", "Username")
password = config.get("Credentials", "Password")
# 更新配置值
config.set("Credentials", "Password", "new_password")
# 保存配置文件
with open("config.ini", "w") as config_file:
config.write(config_file)asyncio example:
import asyncio
# 定义异步任务
async def hello():
print("Hello")
await asyncio.sleep(1)
print("World")
# 创建事件循环
loop = asyncio.get_event_loop()
# 执行异步任务
loop.run_until_complete(hello())
loop.close()The article concludes that Python’s extensive third‑party ecosystem offers developers a wealth of tools to accelerate development, enhance functionality, and address a wide range of programming challenges.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.