pythonrdkit

Not able to import module after adding folder to path


I am trying to import a module based on this RDKit blog: https://greglandrum.github.io/rdkit-blog/posts/2023-12-01-using_sascore_and_npscore.html but it won't work if I try to include it in a separate file. The following works in a jupyter cell:

try:
    from rdkit.Contrib.SA_Score import sascorer
except ImportError:
    import sys
    import os
    sys.path.append(os.path.join(os.environ['CONDA_PREFIX'],'share','RDKit','Contrib', 'SA_score'))

    import sascorer

However, if I include that in a python script and define some other function, trying to import that function gives me an ModuleNotFoundError.


Solution

  • I guess there may be something lacking between os.environ['CONDA_PREFIX'] and "share".

    I wrote a python script test.py

    import os
    import sys
    
    import rdkit
    from rdkit import Chem
    from rdkit.Chem import RDConfig
    
    def main():
        sys.path.append(os.path.join(RDConfig.RDContribDir, "SA_Score"))
        import sascorer
    
        print("RDKit version:", rdkit.__version__)
        print("RDConfig.RDContribDir:", RDConfig.RDContribDir)
        print("done")
    
    if __name__=="__main__":
        main()
    

    and I got

    $ python test.py
    RDKit version: 2024.03.3
    RDConfig.RDContribDir: /home/username/apps/miniforge3/envs/rdkit/share/RDKit/Contrib
    done
    

    cf.
    https://sishida21.github.io/2021/08/07/rdkit-sascore-calculation-caution/
    (in Japanese)