This is a read-only mirror of pymolwiki.org

Difference between revisions of "Python Integration"

From PyMOL Wiki
Jump to navigation Jump to search
m (2 revisions)
m (python3)
Line 23: Line 23:
  
 
==Launching PyMOL from Python programs==
 
==Launching PyMOL from Python programs==
Running PyMOL from a Python script requires two commands:
 
<source lang="python">
 
import pymol
 
pymol.finish_launching()
 
</source>
 
  
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:
+
See [[Launching From a Script]].
<source lang="python">
 
import __main__
 
__main__.pymol_argv = ['pymol','-qc'] # Pymol: quiet and no GUI
 
from time import sleep
 
import pymol
 
pymol.finish_launching()
 
 
 
for index in [1,2,3]:
 
    pymol.cmd.reinitialize()
 
    # Desired pymol commands here to produce and save figures
 
    sleep(0.5) # (in seconds)
 
</source>
 
  
 
==Using the PyMOL commandline==
 
==Using the PyMOL commandline==
Line 47: Line 30:
 
Are you aware that the PyMOL command line is also a Python command line? You can just use PyMOL interactively in that fashion.
 
Are you aware that the PyMOL command line is also a Python command line? You can just use PyMOL interactively in that fashion.
 
<source lang="python">
 
<source lang="python">
PyMOL>print 1+1
+
PyMOL>print(1+1)
 
2
 
2
 
PyMOL>from random import random
 
PyMOL>from random import random
PyMOL>print random()
+
PyMOL>print(random())
 
0.739460642143
 
0.739460642143
 +
</source>
  
 
The only major difference is that the default namespace for PyMOL is "pymol" not "__main__"
 
The only major difference is that the default namespace for PyMOL is "pymol" not "__main__"
  
PyMOL>print __name__
+
<source lang="python">
 +
PyMOL>print(__name__)
 
pymol
 
pymol
 
</source>
 
</source>

Revision as of 06:35, 12 February 2020

Launching Python programs from PyMOL

Running a Python script from PyMOL, usually the command:

run script.py

Is enough. Of course, the file script.py 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 example.py     # synchronous, in PyMOL module
pymol -r example.py  # synchronous in __main__ module
pymol -l example.py  # asychronous in a new module

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

run example.py        # synchronous in pymol module
run example.py,main   # synchronous in __main__ module

spawn example.py        # asychronous in a new module
spawn example.py,global # asychronous in the PyMOL module
spawn example.py,main   # asychronous in the __main__ module

Launching PyMOL from Python programs

See Launching From a Script.

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)
2
PyMOL>from random import random
PyMOL>print(random())
0.739460642143

The only major difference is that the default namespace for PyMOL is "pymol" not "__main__"

PyMOL>print(__name__)
pymol