Search code examples
chemistryrdkitmolecule

AttributeError: 'Mol' object has no attribute 'Compute2DCoords'


import rdkit.Chem as Chem

mol = Chem.MolFromSmiles('CCC') 
mol.Compute2DCoords()

Traceback (most recent call last):    
  File "/Users/lukaskaspras/Projekte_2024/Merck/dash-direct-to-biology/playground/playing_around.py", line 46, in <module>
    mol.Compute2DCoords()
    ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Mol' object has no attribute 'Compute2DCoords'

RDKIT's documentation on Chem.Mol gave me the impression that Compute2DCoords was a method of the Chem.Mol class. This, however, is not the case due to the internal structure of RDKIT.

See the answer below:


Solution

  • Solution is quite simple; explicitly import rdkit.Chem.AllChem as this import modifies (specifically: extends) the functionality of Mol:

    import rdkit.Chem as Chem
    from rdkit.Chem import AllChem
    
    mol = Chem.MolFromSmiles('CCC') 
    mol.Compute2DCoords()
    

    Explanation from Michał Krompiec - see here:

    Actually, this is expected given the fact that Python translates object.method() to method(object). Hence, m.Compute2DCoords(), although "incorrect" (because Compute2DCoords() is not a method of the Mol class), is valid Python code and is understood as Compute2DCoords(m). And this won't work unless AllChem is loaded.