PyMol Python API

This section provides a tutorial on how to use PyMol Python API to invoke PyMol commands from Python scripts.

PyMol also provides a Python API that allows you to access all PyMol functionalities from a Python script.

All PyMol commands are wrapped as Python functions in the pymol.com class packaged in the "pymol" module. You call those functions to perform any molicule visualization tasks.

For example, the following Python script will rotate the viewing camera 360 degrees in 1 second in a loop:

# Rotate-Camera.py
import pymol
import time

for i in range(1, 21):
   print("Moving...")
   pymol.cmd.turn('y', 18)
   time.sleep(0.05)

One way to execute this script is to use the "run" command at the PyMOL command prompt:

PyMOL>run Rotate-Camera.py

If you try the above command, you probably expect the molecule structure view will be rotated a full circle in 20 steps in 1 second time period. However, PyMol will actuall freeze the viewer for 1 second and display the structure once at the end.

I think the "run" command is actually executing the entire Python script in the background without updating the structure image in the viewer during the execution.

When you use the "run python-script-file" command at the PyMOL command prompt, you are using the PyMOL interpreter to execute the Python script, which calls back PyMol API functions.

See the next tutorial on how to use Python interpreter to execute Python scripts that calls PyMol API functions.

You can also read PyMol Python API reference on line at pymol.org/dokuwiki/doku.php?id=api.

Table of Contents

 About This Book

 Introduction of Molecules

 Molecule Names and Identifications

 Protein and Amino Acid

 Nucleobase, Nucleoside, Nucleotide, DNA and RNA

 Gene and Chromosome

 Protein Kinase (PK)

 SDF (Structure Data File)

 RDKit: Open-Source Cheminformatics Software

 PyMol Installation

 PyMol GUI and CLI

 PyMol Selections

 PyMol Editing Functions

 PyMol Measurement Functions

 PyMol Movie Functions

PyMol Python Integration

 Run Python Statements from PyMol

PyMol Python API

 Launch PyMol from Python Interpreter

 PyMol Python API Only Functions

 PyMol Object Functions

 ChEMBL Database - European Molecular Biology Laboratory

 PubChem Database - National Library of Medicine

 PDB (Protein Data Bank)

 INSDC (International Nucleotide Sequence Database Collaboration)

 Resources and Tools

 Molecule Related Terminologies

 References

 Full Version in PDF/EPUB