Call current PyMOL session from python script
Question: I'm trying to call current PyMOL session from python script (wxpython GUI),
and then load some data from PyMOL and send few commands to PyMOL. At the
moment I can open a new PyMOL session in python script:

    
    
    import sys, os
    from wx import *
    app = wx.App()
    dialog = wx.FileDialog ( None, message = 'Set PyMOL directiry', style = wx.OPEN)
    if dialog.ShowModal() == wx.ID_OK:
        # Pymol path
        moddir = dialog.GetDirectory()
        sys.path.insert(0, moddir)
        os.environ['PYMOL_PATH'] = moddir
    
        import pymol
        pymol.finish_launching()     
    else:
        print 'Nothing was selected.'
    
    from pymol import *   
    dialog1 = wx.FileDialog ( None, message = 'Set PDB file', style = wx.OPEN)
    if dialog1.ShowModal() == wx.ID_OK:
        pdbfile = dialog1.GetPath()
        cmd.load(pdbfile)
    else:
        print 'Nothing was selected.'
    
    dialog.Destroy()
    app.MainLoop() 
    

BUT actually I'd like to check in my python scrip whether any PyMOL session is
already opened. I found discussion corresponding to this topic here: [Only
call function if PyMOL
running](http://stackoverflow.com/questions/11858681/only-call-function-if-
pymol-running) Following this discussion I tried to call 'print' function in
PyMOL:

    
    
    try:
        from pymol import cmd
        print 'Connected'
    except:
        <open new Pymol sesssion>
    

but I do not see any text in PyMOL cmd. I tried to determine PyMOL path before
this statement and again I failed.

Does anyone know how to call current PyMOL session from python script?


Answer: To my knowledge, there is no way to interact with an existing PyMOL session
from the Python interpreter. There are a few alternatives:

  1. The existing PyMOL command prompt accepts valid Python and you can [run any script](http://www.pymolwiki.org/index.php/Running_Scripts) directly from the prompt. Perhaps you could execute your script from there instead?

  2. You can start up a new PyMOL session as part of your script. PyMOL's `-i` flag may be especially helpful- it enables you to work in a headless environment:
    
        -i     Disable the internal OpenGL GUI (object list, menus, etc.)
    

  3. Take a careful look at PyMOL's `__init.py__`. You'll find the intricacies of PyMOL's threading there. Perhaps you can find something useful to manipulate?

### Side note:

Calling `print` from your script will _not_ make the text appear in the PyMOL
session, it will merely write the text to standard output (i.e. it will be
printed to your terminal).

* * *

The following PyMOL wiki pages may be of help to you:

  * [Running scripts](http://www.pymolwiki.org/index.php/Running_Scripts)
  * [Launching PyMOL](http://www.pymolwiki.org/index.php/Category%3aLaunching)
  * [Launching from a script](http://www.pymolwiki.org/index.php/Launching_From_a_Script)

