Index: run.py
===================================================================
--- run.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ run.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -126,4 +126,4 @@
 if __name__ == "__main__":
     prepare()
-    from sas.sasview.sasview import run
-    run()
+    from sas.sasview.sasview import run_gui
+    run_gui()
Index: sasview/installer_generator.py
===================================================================
--- sasview/installer_generator.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ sasview/installer_generator.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -4,7 +4,11 @@
 exists in the current directory. Edit local_config.py according to your needs.
 """
+import os
+import sys
+import string
+
+root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+sys.path.insert(0, os.path.join(root, 'sasview-install', 'Lib', 'site-packages'))
 from sas.sasview import local_config
-import os 
-import string
 
 REG_PROGRAM = """{app}\MYPROG.EXE"" ""%1"""
@@ -13,5 +17,5 @@
 AppVerName = str(local_config.__appname__ )+'-'+ str(local_config.__version__)
 Dev = ''
-if AppVerName.lower().count('dev') > 0:
+if 'dev' in AppVerName.lower():
     Dev = '-Dev'
 AppPublisher = local_config._copyright
Index: sasview/sasview_console.py
===================================================================
--- sasview/sasview_console.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
+++ sasview/sasview_console.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -0,0 +1,2 @@
+from sas.sasview.sasview import run_cli
+run_cli()
Index: sasview/sasview_gui.py
===================================================================
--- sasview/sasview_gui.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
+++ sasview/sasview_gui.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -0,0 +1,2 @@
+from sas.sasview.sasview import run_gui
+run_gui()
Index: sasview/setup_exe.py
===================================================================
--- sasview/setup_exe.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ sasview/setup_exe.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -33,7 +33,7 @@
 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 platform = '%s-%s'%(get_platform(), sys.version[:3])
-build_path = os.path.join(root, 'build', 'lib.'+platform)
-#build_path = os.path.join(root, 'sasview-install', 'Lib', 'site-packages')
+build_path = os.path.join(root, 'sasview-install', 'Lib', 'site-packages')
 sys.path.insert(0, build_path)
+doc_path = os.path.join(root, 'build', 'lib.'+platform, 'doc')
 
 from sas.sasview import local_config
@@ -279,5 +279,4 @@
 
 # See if the documentation has been built, and if so include it.
-doc_path = os.path.join(build_path, "doc")
 if os.path.exists(doc_path):
     for dirpath, dirnames, filenames in os.walk(doc_path):
@@ -314,5 +313,8 @@
     ])
 packages.append('periodictable.core') # not found automatically
-#packages.append('IPython')
+# For an interactive interpreter, SasViewCom
+packages.extend(['IPython','pyreadline','pyreadline.unicode_helper'])
+
+# individual models
 includes = ['site', 'lxml._elementpath', 'lxml.etree']
 
@@ -329,5 +331,6 @@
     'tcl84.dll', 'tk84.dll', 'QtGui4.dll', 'QtCore4.dll',
     # numpy 1.8 openmp bindings (still seems to use all the cores without them)
-    'libiomp5md.dll', 'libifcoremd.dll', 'libmmd.dll', 'svml_dispmd.dll','libifportMD.dll',
+    # ... but we seem to need them when building from anaconda, so don't exclude ...
+    #'libiomp5md.dll', 'libifcoremd.dll', 'libmmd.dll', 'svml_dispmd.dll','libifportMD.dll',
     # microsoft C runtime (not allowed to ship with the app; need to ship vcredist
     'msvcp90.dll',
@@ -340,11 +343,29 @@
     ]
 
+# extra dlls not found automatically
+python_root = os.path.dirname(os.path.abspath(sys.executable))
+dll_path = os.path.join(python_root, 'Library', 'bin')
+# the following are found already: 'mkl_intel_thread', 'libmmd', 'libifcoremd'
+dll_includes = ['mkl_core', 'mkl_def', 'libiomp5md']
+dll_includes = [os.path.join(dll_path, dll+'.dll') for dll in dll_includes]
+dll_includes = [dll for dll in dll_includes if os.path.exists(dll)]
+if dll_includes:
+   DATA_FILES.append(('.', dll_includes))
+
 target_wx_client = Target(
     description = 'SasView',
-    script = 'startup.py',
+    script = 'sasview_gui.py',
     icon_resources = [(1, local_config.SetupIconFile_win)],
     other_resources = [(24, 1, manifest)],
     dest_base = "SasView"
     )
+
+target_console_client = Target(
+    description = 'SasView console',
+    script = 'sasview_console.py',
+    icon_resources = [(1, local_config.SetupIconFile_win)],
+    other_resources = [(24, 1, manifest)],
+    dest_base = "SasViewCom"
+)
 
 bundle_option = 2
@@ -358,5 +379,5 @@
 setup(
     windows=[target_wx_client],
-    console=[],
+    console=[target_console_client],
     options={
         'py2exe': {
Index: sasview/setup_mac.py
===================================================================
--- sasview/setup_mac.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ sasview/setup_mac.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -175,5 +175,5 @@
 APPNAME = "SasView "+VERSION
 DMGNAME = "SasView-"+VERSION+"-MacOSX"
-APP = ['sasview.py']
+APP = ['sasview_gui.py']
 
 EXCLUDES = ['PyQt4', 'sip', 'QtGui']
Index: sview/startup.py
===================================================================
--- sasview/startup.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ 	(revision )
@@ -1,2 +1,0 @@
-from sas.sasview.sasview import run
-run()
Index: setup.py
===================================================================
--- setup.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ setup.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -56,13 +56,11 @@
     #             file_path =  os.path.join(f_path, f)
     #             os.remove(file_path)
-    if os.path.exists(SASVIEW_BUILD):
-        print "Removing existing build directory", SASVIEW_BUILD, "for a clean build"
-        shutil.rmtree(SASVIEW_BUILD)
+
+if os.path.exists(SASVIEW_BUILD):
+    print "Removing existing build directory", SASVIEW_BUILD, "for a clean build"
+    shutil.rmtree(SASVIEW_BUILD)
                     
 # 'sys.maxsize' and 64bit: Not supported for python2.5
-is_64bits = False
-if sys.version_info >= (2, 6):
-    is_64bits = sys.maxsize > 2**32
-    
+is_64bits = sys.maxsize > 2**32
 enable_openmp = False
 
@@ -338,10 +336,10 @@
     zip_safe = False,
     entry_points = {
-                    'console_scripts':[
-                                       "sasview = sas.sasview.sasview:run",
-                                       ]
-                    },
-    cmdclass = {'build_ext': build_ext_subclass,
-                'docs': BuildSphinxCommand,
-                'disable_openmp': DisableOpenMPCommand}
+        'console_scripts': ["sasview = sas.sasview.sasview:run_cli"]
+        },
+    cmdclass = {
+        'build_ext': build_ext_subclass,
+        'docs': BuildSphinxCommand,
+        'disable_openmp': DisableOpenMPCommand,
+        }
     )   
Index: src/sas/sasview/__init__.py
===================================================================
--- src/sas/sasview/__init__.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ src/sas/sasview/__init__.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -1,2 +1,2 @@
-__version__ = "4.0.0-a3"
+__version__ = "4.0.0a4"
 __build__ = "1"
Index: src/sas/sasview/sasview.py
===================================================================
--- src/sas/sasview/sasview.py	(revision efe730d4bba7a05c203957e4495eece8359fcd77)
+++ src/sas/sasview/sasview.py	(revision 899e084c5a4f0e16bee8152f20438288d95e87ec)
@@ -16,10 +16,8 @@
 import traceback
 
-log_file = os.path.join(os.path.expanduser("~"), 'sasview.log')
-logging.basicConfig(level=logging.INFO,
-                    format='%(asctime)s %(levelname)s %(message)s',
-                    filename=log_file,
-)
-logging.captureWarnings(True)
+
+reload(sys)
+sys.setdefaultencoding("iso-8859-1")
+
 
 class StreamToLogger(object):
@@ -41,56 +39,4 @@
             self.logger.log(self.log_level, line.rstrip())
 
-stderr_logger = logging.getLogger('STDERR')
-sl = StreamToLogger(stderr_logger, logging.ERROR)
-sys.stderr = sl
-
-# Log the start of the session
-logging.info(" --- SasView session started ---")
-
-# Log the python version
-logging.info("Python: %s" % sys.version)
-
-# Allow the dynamic selection of wxPython via an environment variable, when devs
-# who have multiple versions of the module installed want to pick between them.
-# This variable does not have to be set of course, and through normal usage will
-# probably not be, but this can make things a little easier when upgrading to a
-# new version of wx.
-WX_ENV_VAR = "SASVIEW_WX_VERSION"
-if WX_ENV_VAR in os.environ:
-    logging.info("You have set the %s environment variable to %s." % \
-                 (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
-    import wxversion
-    if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
-        logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
-        wxversion.select(os.environ[WX_ENV_VAR])
-    else:
-        logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
-else:
-    logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
-
-import wx
-
-try:
-    logging.info("Wx version: %s" % wx.__version__)
-except:
-    logging.error("Wx version: error reading version")
-
-from . import wxcruft
-wxcruft.call_later_fix()
-#wxcruft.trace_new_id()
-#Always use private .matplotlib setup to avoid conflicts with other
-#uses of matplotlib
-
-import sas.sasgui
-mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib')
-if not os.path.exists(mplconfigdir):
-    os.mkdir(mplconfigdir)
-os.environ['MPLCONFIGDIR'] = mplconfigdir
-reload(sys)
-sys.setdefaultencoding("iso-8859-1")
-
-
-from sas.sasgui.guiframe import gui_manager
-from .welcome_panel import WelcomePanel
 PLUGIN_MODEL_DIR = 'plugin_models'
 APP_NAME = 'SasView'
@@ -103,6 +49,6 @@
         """
         """
-        #from gui_manager import ViewApp
-        self.gui = gui_manager.SasViewApp(0)
+        from sas.sasgui.guiframe.gui_manager import SasViewApp
+        self.gui = SasViewApp(0)
         # Set the application manager for the GUI
         self.gui.set_manager(self)
@@ -116,5 +62,5 @@
         # Fitting perspective
         try:
-            import sas.sasgui.perspectives.fitting as module    
+            import sas.sasgui.perspectives.fitting as module
             fitting_plug = module.Plugin()
             self.gui.add_perspective(fitting_plug)
@@ -142,5 +88,5 @@
             logging.error(traceback.format_exc())
 
-        #Calculator perspective   
+        #Calculator perspective
         try:
             import sas.sasgui.perspectives.calculator as module
@@ -154,4 +100,5 @@
 
         # Add welcome page
+        from .welcome_panel import WelcomePanel
         self.gui.set_welcome_panel(WelcomePanel)
 
@@ -164,5 +111,70 @@
 
 
-def run():
+def setup_logging():
+    log_file = os.path.join(os.path.expanduser("~"), 'sasview.log')
+    logging.basicConfig(level=logging.INFO,
+                        format='%(asctime)s %(levelname)s %(message)s',
+                        filename=log_file,
+                        )
+    logging.captureWarnings(True)
+
+    stderr_logger = logging.getLogger('STDERR')
+    sl = StreamToLogger(stderr_logger, logging.ERROR)
+    sys.stderr = sl
+
+    # Log the start of the session
+    logging.info(" --- SasView session started ---")
+
+    # Log the python version
+    logging.info("Python: %s" % sys.version)
+
+
+def setup_wx():
+    # Allow the dynamic selection of wxPython via an environment variable, when devs
+    # who have multiple versions of the module installed want to pick between them.
+    # This variable does not have to be set of course, and through normal usage will
+    # probably not be, but this can make things a little easier when upgrading to a
+    # new version of wx.
+    WX_ENV_VAR = "SASVIEW_WX_VERSION"
+    if WX_ENV_VAR in os.environ:
+        logging.info("You have set the %s environment variable to %s." % \
+                     (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
+        import wxversion
+        if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
+            logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
+            wxversion.select(os.environ[WX_ENV_VAR])
+        else:
+            logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
+    else:
+        logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
+
+    import wx
+
+    try:
+        logging.info("Wx version: %s" % wx.__version__)
+    except:
+        logging.error("Wx version: error reading version")
+
+    from . import wxcruft
+    wxcruft.call_later_fix()
+    #wxcruft.trace_new_id()
+    #Always use private .matplotlib setup to avoid conflicts with other
+    #uses of matplotlib
+
+
+def setup_mpl():
+    import sas.sasgui
+    mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib')
+    if not os.path.exists(mplconfigdir):
+        os.mkdir(mplconfigdir)
+    os.environ['MPLCONFIGDIR'] = mplconfigdir
+    # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override
+    # mpl.use().  Note: Don't import matplotlib here since the script that
+    # we are running may not actually need it; also, putting as little on the
+    # path as we can
+    os.environ['MPLBACKEND'] = 'WXAgg'
+
+
+def run_gui():
     """
     __main__ method for loading and running SasView
@@ -170,10 +182,19 @@
     from multiprocessing import freeze_support
     freeze_support()
-    if len(sys.argv) > 1:
-        ## Run sasview as an interactive python interpreter
-        if sys.argv[1] == "-i":
-            sys.argv = ["ipython", "--pylab"]
-            from IPython import start_ipython
-            sys.exit(start_ipython())
+    setup_logging()
+    setup_wx()
+    setup_mpl()
+    SasView()
+
+
+def run_cli():
+    setup_mpl()
+    if len(sys.argv) == 1:
+        # Run sasview as an interactive python interpreter
+        sys.argv = ["ipython", "--pylab"]
+        from IPython import start_ipython
+        sys.exit(start_ipython())
+    else:
+        # Run sasview as a python script interpreter
         thing_to_run = sys.argv[1]
         sys.argv = sys.argv[1:]
@@ -183,8 +204,6 @@
         else:
             runpy.run_module(thing_to_run, run_name="__main__")
-    else:
-        SasView()
 
 if __name__ == "__main__":
-    run()
-
+    run_gui()
+
