From PyMOLWiki
Revision as of 14:46, 25 March 2020 by Jaredsampson (talk | contribs) (Basic usage: clarify about relative paths)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

PyMOL Script

PyMOL scripts are plain text files containing PyMOL commands, one per line, appearing as they would be entered at the PyMOL command prompt. The standard extension for a PyMOL script is .pml.

Basic usage

Save your commands in a plain text file (using e.g. Notepad, TextEdit, or a text editor of your choice) with a .pml extension, e.g. script.pml.

Run the script by either File > Run script... in the GUI menu bar, or via the command


Relative paths are fine, so if PyMOL has been launched in the same directory as your script, you can simplify this to @script.pml

Command syntax

PyMOL commands are typically in the form:

command arg1[, arg2[, ...]]

For example:

color green, chain A

which will, as you might expect, color all the atoms of chain A green. There is no comma between the command and the first argument, and in general, no quotation marks are necessary around the arguments. (Exceptions to this rule include situations where you are passing a text string as an argument which contains a comma; in this case the entire string should be quoted.)

For commands that take keyword arguments, the syntax is similar to passing keyword arguments to a Python function:

fetch 1nmr, async=0

will use the asynchronous version of fetch, waiting to execute any following commands until the fetch command finishes.

Python code

PyMOL scripts can also contain Python code. Single-line Python statements can be written normally and will be handled as expected;

# imports
import this
from glob import glob

# variable assignments
prefix = "structure_"

# complex single-line statements like list comprehensions
[cmd.load(x) for x in glob(f'{prefix}*.pdb')]

# any other arbitrary Python code
print(2 + 2)

Python code that spans more than one line can be enclosed in a python/python end code block, like this:

def hello(name="World"):
    print("Hello,", name)
python end

Handy commands to include in your scripts

  • reinitialize - issue this command at the start of your script to allow it to be run multiple times in the same PyMOL session.
  • log_open log.pml - create a log file to record actions taken in the GUI. Examine this log file to learn new commands, although keep in mind that these are typically formatted as Python function calls.
  • deselect - place this at the very end of the script to ensure no atoms are selected after the script completes, for a cleaner GUI appearance.