Python Integration

From PyMOLWiki
Jump to: navigation, search

Launching Python programs from PyMOL

Running a Python script from PyMOL, usually the command:


Is enough. Of course, the file needs to be in the working directory. You can also launch Python scripts when starting PyMOL. Asynchronous means, that a new Python thread is started:

pymol     # synchronous, in PyMOL module
pymol -r  # synchronous in __main__ module
pymol -l  # asychronous in a new module

You can also launch python programs from within PyMOL with the commands:

run        # synchronous in pymol module
run,main   # synchronous in __main__ module
spawn        # asychronous in a new module
spawn,global # asychronous in the PyMOL module
spawn,main   # asychronous in the __main__ module

Launching PyMOL from Python programs

Running PyMOL from a Python script requires two commands:

import pymol

If using a single script to produce multiple figures, then the reinitialize() command is necessary between parts of the script. This can be made to run silently (without launching the PyMOL GUI) by using the __main__ commands. It is sometimes also necessary to put in a small delay (sleep) in order to allow PyMOL to finish processing the prior step. An example script might be:

import __main__
__main__.pymol_argv = ['pymol','-qc'] # Pymol: quiet and no GUI
from time import sleep
import pymol
for index in [1,2,3]:
    # Desired pymol commands here to produce and save figures
    sleep(0.5) # (in seconds)

Using the PyMOL commandline

Are you aware that the PyMOL command line is also a Python command line? You can just use PyMOL interactively in that fashion.

PyMOL>print 1+1
PyMOL>from random import random
PyMOL>print random()
The only major difference is that the default namespace for PyMOL is "pymol" not "__main__"
PyMOL>print __name__
Personal tools