This is a read-only mirror of pymolwiki.org

Difference between revisions of "PythonTerminal"

From PyMOL Wiki
Jump to navigation Jump to search
m (6 revisions)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
===DESCRIPTION===
+
PyMOL allows the execution of python commands from the PyMOL command line.  It is very useful for both debugging, and for discovering new functions.
This is a small script to allow the execution of python commands from the PyMOL command line.  It is very useful for both debugging, and for discovering new functions.
+
 
 +
* Any expression that is not recognized as a PyMOL command is passed to the underlying python interpreter
 +
* To force a one-line expression to be executed as python, begin the line with a slash (/)
 +
* Use the [[python]] command to input multi-line python code
 +
 
 +
== Examples ==
  
===USAGE===
 
 
<source lang="python">
 
<source lang="python">
py (python commands)
+
# there is no "print" command in PyMOL, so this will go to the python interpreter
</source>
+
print "Hello World (1)"
  
 +
# same, but force it to be python
 +
/print "Hello World (2)"
  
<source lang="python">
+
# no lets trick this system by introducing a PyMOL command named "print"
from pymol import cmd
+
cmd.extend('print', lambda msg: sys.stdout.write("You gave me `%s`\n" % (msg)))
  
def parse(command):
+
# see what happens
exec command
+
print "Hello World (3)"
  
cmd.extend('py', parse)
+
# this will still go to the python interpreter
 +
/print "Hello World (4)"
 
</source>
 
</source>
[[Category:Script_Library]]
+
 
 +
[[Category:System_Scripts]]

Latest revision as of 03:53, 28 March 2014

PyMOL allows the execution of python commands from the PyMOL command line. It is very useful for both debugging, and for discovering new functions.

  • Any expression that is not recognized as a PyMOL command is passed to the underlying python interpreter
  • To force a one-line expression to be executed as python, begin the line with a slash (/)
  • Use the python command to input multi-line python code

Examples

# there is no "print" command in PyMOL, so this will go to the python interpreter
print "Hello World (1)"

# same, but force it to be python
/print "Hello World (2)"

# no lets trick this system by introducing a PyMOL command named "print"
cmd.extend('print', lambda msg: sys.stdout.write("You gave me `%s`\n" % (msg)))

# see what happens
print "Hello World (3)"

# this will still go to the python interpreter
/print "Hello World (4)"