PubChemPy

1.0.5 · active · verified Thu Apr 09

PubChemPy is a simple Python wrapper around the PubChem PUG REST API, providing an intuitive interface to query chemical information from PubChem. It allows programmatic access to compounds, substances, assays, and their properties. The current version is 1.0.5, and releases are infrequent, primarily addressing bug fixes and minor enhancements.

Warnings

Install

Imports

Quickstart

This example demonstrates how to search for a compound by name, retrieve its basic properties, and then fetch additional specific properties using its PubChem CID.

import pubchempy as pcp

try:
    # Search for compounds by name
    compounds = pcp.get_compounds('aspirin', 'name')

    if compounds:
        aspirin = compounds[0]
        print(f"Compound Name: {aspirin.iupac_name}")
        print(f"CID: {aspirin.cid}")
        print(f"Molecular Formula: {aspirin.molecular_formula}")
        print(f"Canonical SMILES: {aspirin.canonical_smiles}")

        # Retrieve specific properties for a compound
        properties = pcp.get_properties(
            ['molecular_weight', 'xlogp'], # List of properties to fetch
            aspirin.cid,
            'cid' # Namespace: 'cid' for compound IDs
        )
        if properties:
            print(f"Molecular Weight: {properties[0]['MolecularWeight']}")
            print(f"XLogP: {properties[0]['XLogP']}")
        else:
            print("Could not retrieve additional properties.")

    else:
        print("Aspirin not found in PubChem.")

except pcp.PubChemPyError as e:
    print(f"A PubChemPy API error occurred: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →