wxPython / wx / py /

#!/usr/bin/env python
"""PyShell is a python shell application."""

# The next two lines, and the other code below that makes use of
# ``__main__`` and ``original``, serve the purpose of cleaning up the
# main namespace to look as much as possible like the regular Python
# shell environment.
import __main__
original = __main__.__dict__.keys()

__author__ = "Patrick K. O'Brien <>"
__cvsid__ = "$Id: 63480 2010-02-14 05:34:39Z RD $"
__revision__ = "$Revision: 63480 $"[11:-2]

import wx
import os

class App(wx.App):
    """PyShell standalone application."""

    def OnInit(self):
        import os
        import wx
        from wx import py

        confDir = wx.StandardPaths.Get().GetUserDataDir()
        if not os.path.exists(confDir):
        fileName = os.path.join(confDir, 'config')
        self.config = wx.FileConfig(localFilename=fileName)

        self.frame =, dataDir=confDir)
        return True

The main() function needs to handle being imported, such as with the
pyshell script that wxPython installs:

    #!/usr/bin/env python

    from import main

def main():
    """The main function for the PyShell program."""
    # Cleanup the main namespace, leaving the App class.
    import __main__
    md = __main__.__dict__
    keepers = original
    for key in md.keys():
        if key not in keepers:
            del md[key]
    # Create an application instance.
    app = App(0)
    # Cleanup the main namespace some more.
    if md.has_key('App') and md['App'] is App:
        del md['App']
    if md.has_key('__main__') and md['__main__'] is __main__:
        del md['__main__']
    # Mimic the contents of the standard Python shell's sys.path.
    import sys
    if sys.path[0]:
        sys.path[0] = ''
    # Add the application object to the sys module's namespace.
    # This allows a shell user to do:
    # >>> import sys
    # >>> = app
    del sys
    # Start the wxPython event loop.

if __name__ == '__main__':