Repository: spyder-ide/spyder

Issue:
IndexError when reading Internal console history file
I have tried : `conda install -f python.app` and I still get the same following error message when I try to launch Spyder 
Traceback (most recent call last):
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/utils.py", line 281, in create_window
main.setup()
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/mainwindow.py", line 875, in setup
PLUGIN_REGISTRY.register_plugin(self, PluginClass,
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/api/plugin_registration/registry.py", line 274, in register_plugin
instance = self._instantiate_spyder5_plugin(
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/api/plugin_registration/registry.py", line 152, in _instantiate_spyder5_plugin
plugin_instance = PluginClass(main_window, configuration=CONF)
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/api/plugins/new_api.py", line 890, in __init__
super().__init__(parent, configuration=configuration)
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/api/plugins/new_api.py", line 294, in __init__
self._container = container = self.CONTAINER_CLASS(
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/plugins/console/widgets/main_widget.py", line 123, in __init__
self.shell = InternalShell( # TODO: Move to use SpyderWidgetMixin?
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/plugins/console/widgets/internalshell.py", line 153, in __init__
super().__init__(parent, get_conf_path('history_internal.py'),
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/plugins/console/widgets/shell.py", line 665, in __init__
ShellBaseWidget.__init__(self, parent, history_filename,
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/plugins/console/widgets/shell.py", line 75, in __init__
self.history = self.load_history()
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/plugins/console/widgets/shell.py", line 503, in load_history
if rawhistory[1] != self.INITHISTORY[1]:
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/farah/opt/anaconda3/bin/spyder", line 11, in 
sys.exit(main())
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/start.py", line 237, in main
mainwindow.main(options, args)
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/mainwindow.py", line 1992, in main
mainwindow = create_window(MainWindow, app, splash, options, args)
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/utils.py", line 283, in create_window
if main.console is not None:
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/mainwindow.py", line 1088, in __getattr__
return self.get_plugin(self._INTERNAL_PLUGINS_MAPPING[attr])
File "/Users/farah/opt/anaconda3/lib/python3.9/site-packages/spyder/app/mainwindow.py", line 165, in get_plugin
raise SpyderAPIError(f'Plugin "{plugin_name}" not found!')
spyder.api.exceptions.SpyderAPIError: Plugin "internal_console" not found!


Discussion:
Hey @Farah9494, thanks for reporting. Please open a terminal and run

    spyder --reset

then start Spyder again. That should do the trick.

However, we'll fix this bug in our next version (5.4.0), to be released in a couple of weeks, so other users don't encounter it.

Resolving patch:
diff --git a/spyder/plugins/console/widgets/shell.py b/spyder/plugins/console/widgets/shell.py
--- a/spyder/plugins/console/widgets/shell.py
+++ b/spyder/plugins/console/widgets/shell.py
@@ -22,9 +22,9 @@
 
 # Third party imports
 from qtpy.compat import getsavefilename
-from qtpy.QtCore import Property, QCoreApplication, Qt, QTimer, Signal, Slot
+from qtpy.QtCore import Property, Qt, QTimer, Signal, Slot
 from qtpy.QtGui import QKeySequence, QTextCharFormat, QTextCursor
-from qtpy.QtWidgets import QApplication, QMenu, QToolTip
+from qtpy.QtWidgets import QApplication, QMenu
 
 # Local import
 from spyder.config.base import _, get_conf_path, get_debug_level, STDERR
@@ -498,10 +498,16 @@ def _key_period(self, text):
     def load_history(self):
         """Load history from a .py file in user home directory"""
         if osp.isfile(self.history_filename):
-            rawhistory, _ = encoding.readlines(self.history_filename)
-            rawhistory = [line.replace('\n', '') for line in rawhistory]
-            if rawhistory[1] != self.INITHISTORY[1]:
-                rawhistory[1] = self.INITHISTORY[1]
+            # This is necessary to catch any error while reading or processing
+            # history_filename, which causes a crash at startup.
+            # Fixes spyder-ide/spyder#19850
+            try:
+                rawhistory, _ = encoding.readlines(self.history_filename)
+                rawhistory = [line.replace('\n', '') for line in rawhistory]
+                if rawhistory[1] != self.INITHISTORY[1]:
+                    rawhistory[1] = self.INITHISTORY[1]
+            except Exception:
+                rawhistory = self.INITHISTORY
         else:
             rawhistory = self.INITHISTORY
         history = [line for line in rawhistory \


Test patch: