Getting started

This page gives a introduction on how to get started with PubChemPy. This assumes you already have PubChemPy installed.

Retrieving a Compound

Retrieving information about a specific Compound in the PubChem database is simple.

Begin by importing PubChemPy:

>>> import pubchempy as pcp

Let’s get the Compound with CID 5090:

>>> c = pcp.Compound.from_cid(5090)

Now we have a Compound object called c. We can get all the information we need from this object:

>>> print c.molecular_formula
>>> print c.molecular_weight
>>> print c.isomeric_smiles
>>> print c.xlogp
>>> print c.iupac_name
>>> print c.synonyms
[u'rofecoxib', u'Vioxx', u'Ceoxx', u'162011-90-7', u'MK 966', ... ]


All the code examples in this documentation will assume you have imported PubChemPy as pcp. If you prefer, you can alternatively import specific functions and classes by name and use them directly:

from pubchempy import Compound, get_compounds
c = Compound.from_cid(1423)
cs = get_compounds('Aspirin', 'name')


What if you don’t know the PubChem CID of the Compound you want? Just use the get_compounds() function:

>>> results = pcp.get_compounds('Glucose', 'name')
>>> print results
[Compound(79025), Compound(5793), Compound(64689), Compound(206)]

The first argument is the identifier, and the second argument is the identifier type, which must be one of name, smiles, sdf, inchi, inchikey or formula. It looks like there are 4 compounds in the PubChem Database that have the name Glucose associated with them. Let’s take a look at them in more detail:

>>> for compound in results:
...    print compound.isomeric_smiles

It looks like they all have different stereochemistry information.

Retrieving the record for a SMILES string is just as easy:

>>> pcp.get_compounds('C1=CC2=C(C3=C(C=CC=N3)C=C2)N=C1', 'smiles')


Beware that line notation inputs like SMILES and InChI can return automatically generated records that aren’t actually present in PubChem, and therefore have no CID and are missing many properties that are too complicated to calculate on the fly.

That’s all the most basic things you can do with PubChemPy. Read on for more some more advanced usage examples.