Skip to main content

Python bindings for PubMed and PMC APIs for retrieving biomedical research articles

Project description

PubMed Client - Python Bindings

PyPI version Python 3.12+ License: MIT

Python bindings for the PubMed and PMC (PubMed Central) API client library.

Overview

This package provides Python bindings to the Rust-based PubMed client library, enabling high-performance access to PubMed and PMC APIs from Python.

Features

  • PubMed API: Search and retrieve article metadata
  • PMC API: Access full-text articles from PubMed Central
  • ELink API: Get related articles, citations, and PMC links
  • SearchQuery Builder: Build complex queries programmatically with filters
  • High Performance: Built with Rust for speed and reliability
  • Type-Safe: Full type hints for better IDE support

Installation

From PyPI

pip install pubmed-client-py

With uv

uv add pubmed-client-py

From Source

# Clone the repository
git clone https://github.com/illumination-k/pubmed-client.git
cd pubmed-client/pubmed-client-py

# Create virtual environment and install
uv venv
uv run --with maturin maturin develop

Quick Start

Basic Usage

import pubmed_client

# Create a unified client
client = pubmed_client.Client()

# Search PubMed
articles = client.pubmed.search_and_fetch("covid-19 vaccine", max_results=10)

for article in articles:
    print(f"Title: {article.title}")
    print(f"PMID: {article.pmid}")
    print(f"Journal: {article.journal}")
    print()

Fetch Single Article

import pubmed_client

client = pubmed_client.Client()

# Fetch article by PMID
article = client.pubmed.fetch_article("31978945")

print(f"Title: {article.title}")
print(f"Authors: {article.author_count}")
print(f"Abstract: {article.abstract_text[:200]}...")

# Access authors and affiliations
for author in article.authors():
    print(f"  {author.full_name}")
    if author.orcid:
        print(f"    ORCID: {author.orcid}")

Fetch PMC Full-Text

import pubmed_client

client = pubmed_client.Client()

# Fetch full text from PMC
full_text = client.pmc.fetch_full_text("PMC7906746")

print(f"Title: {full_text.title}")
print(f"Sections: {len(full_text.sections())}")
print(f"References: {len(full_text.references())}")

# Access article sections
for section in full_text.sections():
    print(f"Section: {section.title}")
    print(f"Content: {section.content[:200]}...")

# Access figures and tables
for figure in full_text.figures():
    print(f"Figure: {figure.label}")
    print(f"Caption: {figure.caption}")

# Convert to Markdown
markdown = full_text.to_markdown()
print(markdown)

Citation Analysis

import pubmed_client

client = pubmed_client.Client()

# Get citations for an article
citations = client.get_citations([31978945])
print(f"Citation count: {len(citations)}")

# Get citing article PMIDs
for citing_pmid in citations.citing_pmids[:10]:
    print(f"Cited by: {citing_pmid}")

Related Articles and PMC Links

import pubmed_client

client = pubmed_client.Client()

# Find related articles
related = client.get_related_articles([31978945])
print(f"Found {len(related.related_pmids)} related articles")

# Check PMC full-text availability
pmc_links = client.get_pmc_links([31978945])
print(f"PMC IDs available: {pmc_links.pmc_ids}")

Using SearchQuery Builder

import pubmed_client

client = pubmed_client.Client()

# Build a complex query
query = (
    pubmed_client.SearchQuery()
    .query("cancer")
    .published_between(2020, 2024)
    .article_type("Clinical Trial")
    .free_full_text_only()
    .limit(50)
)

# Execute the search
articles = client.pubmed.search_and_fetch(query, 0)  # limit ignored when using SearchQuery

for article in articles:
    print(f"[{article.pmid}] {article.title}")

Boolean Query Combinations

import pubmed_client

# Build complex queries with boolean logic
q1 = pubmed_client.SearchQuery().query("covid-19")
q2 = pubmed_client.SearchQuery().query("vaccine")
q3 = pubmed_client.SearchQuery().query("efficacy")

# Combine with AND
combined = q1.and_(q2).and_(q3)
print(combined.build())  # ((covid-19) AND (vaccine)) AND (efficacy)

# Combine with OR
either = q1.or_(q2)
print(either.build())  # (covid-19) OR (vaccine)

# Exclude specific terms
base = pubmed_client.SearchQuery().query("treatment")
excluded = pubmed_client.SearchQuery().query("animal studies")
human_only = base.exclude(excluded)
print(human_only.build())  # (treatment) NOT (animal studies)

Extract Figures from PMC Articles

import pubmed_client

client = pubmed_client.Client()

# Download and extract figures with captions
figures = client.pmc.extract_figures_with_captions("PMC7906746", "./output")

for fig in figures:
    print(f"Figure: {fig.figure.label}")
    print(f"Caption: {fig.figure.caption}")
    print(f"File: {fig.extracted_file_path}")
    print(f"Size: {fig.file_size} bytes")
    if fig.dimensions:
        print(f"Dimensions: {fig.dimensions[0]}x{fig.dimensions[1]}")

Configuration

With API Key

Using an NCBI API key increases rate limits from 3 to 10 requests per second:

import pubmed_client

config = (
    pubmed_client.ClientConfig()
    .with_api_key("your_ncbi_api_key")
    .with_email("your@email.com")
    .with_tool("YourAppName")
)

client = pubmed_client.Client.with_config(config)

Rate Limiting

import pubmed_client

config = (
    pubmed_client.ClientConfig()
    .with_rate_limit(2.0)  # 2 requests per second
    .with_timeout_seconds(60)  # 60 second timeout
)

client = pubmed_client.Client.with_config(config)

Type Hints and IDE Support

The package includes complete type stubs (.pyi files) for full IDE autocomplete and type checking:

import pubmed_client

# Type hints work automatically
config: pubmed_client.ClientConfig = pubmed_client.ClientConfig()
client: pubmed_client.Client = pubmed_client.Client.with_config(config)
articles: list[pubmed_client.PubMedArticle] = client.pubmed.search_and_fetch("query", 10)

Run type checking with mypy:

mypy your_script.py

Development

Prerequisites

  • Python >= 3.12
  • Rust toolchain (installed via rustup)
  • uv (for Python package management)
  • maturin (for building Python bindings)

Setup Development Environment

# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create virtual environment
cd pubmed-client-py
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Build and install in development mode
uv run --with maturin maturin develop

Running Tests

# Install dev dependencies
uv sync --group dev

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=pubmed_client

Code Quality

# Format code
uv run ruff format

# Lint code
uv run ruff check

# Type checking
uv run mypy tests/

Building

# Build wheel
uv run --with maturin maturin build --release

# Build for distribution
uv run --with maturin maturin build --release --sdist

Publishing

# Publish to PyPI (requires credentials)
uv run --with maturin maturin publish

License

MIT

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pubmed_client_py-0.2.0.tar.gz (787.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pubmed_client_py-0.2.0-cp314-cp314-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.14Windows x86-64

pubmed_client_py-0.2.0-cp314-cp314-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pubmed_client_py-0.2.0-cp314-cp314-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pubmed_client_py-0.2.0-cp313-cp313-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.13Windows x86-64

pubmed_client_py-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pubmed_client_py-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

pubmed_client_py-0.2.0-cp312-cp312-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.12Windows x86-64

pubmed_client_py-0.2.0-cp312-cp312-manylinux_2_38_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ x86-64

pubmed_client_py-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pubmed_client_py-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file pubmed_client_py-0.2.0.tar.gz.

File metadata

  • Download URL: pubmed_client_py-0.2.0.tar.gz
  • Upload date:
  • Size: 787.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pubmed_client_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a3bac67bcfcfc5b03131f3d7ac3db15992e1bc742d5601adae28a994ab97d3d8
MD5 e231bcbb55a232904a5f81dfa788f57f
BLAKE2b-256 d99c01064ee375dcc40219b55ac08e047ad0dc4e69b580fce613982704f3bd83

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0.tar.gz:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0f2c7393aa64c746aced17d25186452ada372428d63e083bef5326eb14d2a171
MD5 39accd9095d2767417d65e32ce33c259
BLAKE2b-256 a9a96f1dd8f9a8ec7c6a34a25804151b59fdba62560fc39926a2ff52ad254bea

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp314-cp314-win_amd64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f942fb6ff2b5cd9e63f3ad8519e1b27dbdf3ca32fd887b903f005ff4d9ff283
MD5 4076c8e7c3211d968eb3e07d57dd5e77
BLAKE2b-256 6144d3d14c584eba2246a493d012f5ba5b2d10d2c390329a2494653af6e58565

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6595a1ffa8470e7ede8458b8f357589c733b5c1ca210baa5f99cdd3cb0c62c01
MD5 880bf5f19e1835dae93ec877bdddfbda
BLAKE2b-256 28d1d524de455f33608bc7d2ef919a12ef98b2958fa8c7501c0da81240cc8009

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp314-cp314-macosx_10_12_x86_64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e3aa23fe22b7cd06ad7459d720812d7a0fe86221dbdbca3250a27bbb78a6d057
MD5 dda8b1fb7926e6489492b05bcf6fd46d
BLAKE2b-256 0cca4140fd6074308e0962d6aa80df908e8b594ca0d9fec5694b9a94ad052c48

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp313-cp313-win_amd64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f7825067c864dd9ab494a6d38845f7b58cd82f99c447eb9e509a35acbb701d41
MD5 7c516e8631d3722e1a2b2baf69437dbe
BLAKE2b-256 310b7d3f96bed485ba36af63f7c9ed71b3bbd205002e69bdf3d9216fe2a19faf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7c70ff25b91fffa0d1ba34fdc9afce1e2af1c59937064a959425902aef89f9bb
MD5 a7bf37722e1d495905c2907fcfefdd7a
BLAKE2b-256 818a7a349af0fd28595be89f13657dd072775c7050cf66378ad2eab9981b6068

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f46e199d8b324efca84cf14ae31199d4c691a2ec5fd07d89a8a77dba340ca1b5
MD5 e922a09b2334c9f7c34cb0e26bbb74c6
BLAKE2b-256 2056950219917787fa8d7c20f4b80fd1d4201088c8ac87772f21007c345aa450

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp312-cp312-win_amd64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp312-cp312-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp312-cp312-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 a619a17aed8a5852e16fe01ffe7efe63716f0a0bae50dd98ce76fba295df23dc
MD5 c1619aba386ec169ef021b3b259d1a4a
BLAKE2b-256 75be5e2b34ecc160082f087a0a13feeb727410b41831b7019698d22979457faf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp312-cp312-manylinux_2_38_x86_64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8acbac918f591327c0092c9698833b73ba036f925c5984411c7f8f90f0e317c0
MD5 5e7c62cbcacd1ffa50f83cf49f50a71f
BLAKE2b-256 aa3f769a83341170c1ad0bf585efaae2d790063cb893c34635fe1d2bcc2e088f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmed_client_py-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pubmed_client_py-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6a62d1d30ad5dc8b443e9f80c3c5a7fc474c2d2c6f6a1bd3b229eea5f3e57c3e
MD5 b1194f656c81734d9027406159702bb9
BLAKE2b-256 6a180664937fc40b1ace8087df37a06d60c4d3775d6e684b9bb5a4a73bb58f59

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmed_client_py-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: release.yml on illumination-k/pubmed-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page