Back to the blog

Obtaining data from a chemical compound with Python

Obtain physicochemical data from a compound directly from PubChem using the pubchempy library, without downloading any dataset manually.

import pubchempy as pcp

# Component: Linoleic Acid
f = pcp.get_compounds("linoleic acid", "name")

# IUPAC name
f1 = f[0].iupac_name
print(f"The IUPAC name is: {f1}")

# Molecular formula
f2 = f[0].molecular_formula
print(f"The molecular formula is: {f2}")

# Molecular weight
f3 = f[0].molecular_weight
print(f"The molecular weight is: {f3}")

Running the script with linoleic acid, we get:

The IUPAC name is: (9Z,12Z)-octadeca-9,12-dienoic acid
The molecular formula is: C18H32O2
The molecular weight is: 280.4

With this pattern you can search by name, CID, SMILES, or InChI any compound in the PubChem catalog and extract properties such as melting point, density, vapor pressure, 2D/3D structure, InChIKey identifiers, etc. It is a very clean way to populate chemical databases without leaving Python.

Installation:

pip install pubchempy
Back to the blog