Navan Chauhan
2 minute readCreated on July 1, 2020Last modified on September 15, 2020

Installing RDKit on Google Colab

RDKit is one of the most integral part of any Cheminfomatic specialist's toolkit but it is notoriously difficult to install unless you already have conda installed. I originally found this in a GitHub Gist but I have not been able to find that gist again :/

Just copy and paste this in a Colab cell and it will install it 👍

import sys import os import requests import subprocess import shutil from logging import getLogger, StreamHandler, INFO logger = getLogger(__name__) logger.addHandler(StreamHandler()) logger.setLevel(INFO) def install( chunk_size=4096, file_name="", url_base="", conda_path=os.path.expanduser(os.path.join("~", "miniconda")), rdkit_version=None, add_python_path=True, force=False): """install rdkit from miniconda ``` import rdkit_installer rdkit_installer.install() ``` """ python_path = os.path.join( conda_path, "lib", "python{0}.{1}".format(*sys.version_info), "site-packages", ) if add_python_path and python_path not in sys.path:"add {} to PYTHONPATH".format(python_path)) sys.path.append(python_path) if os.path.isdir(os.path.join(python_path, "rdkit")):"rdkit is already installed") if not force: return"force re-install") url = url_base + file_name python_version = "{0}.{1}.{2}".format(*sys.version_info)"python version: {}".format(python_version)) if os.path.isdir(conda_path): logger.warning("remove current miniconda") shutil.rmtree(conda_path) elif os.path.isfile(conda_path): logger.warning("remove {}".format(conda_path)) os.remove(conda_path)'fetching installer from {}'.format(url)) res = requests.get(url, stream=True) res.raise_for_status() with open(file_name, 'wb') as f: for chunk in res.iter_content(chunk_size): f.write(chunk)'done')'installing miniconda to {}'.format(conda_path)) subprocess.check_call(["bash", file_name, "-b", "-p", conda_path])'done')"installing rdkit") subprocess.check_call([ os.path.join(conda_path, "bin", "conda"), "install", "--yes", "-c", "rdkit", "python=={}".format(python_version), "rdkit" if rdkit_version is None else "rdkit=={}".format(rdkit_version)])"done") import rdkit"rdkit-{} installation finished!".format(rdkit.__version__)) if __name__ == "__main__": install()
Tagged with: