Skip to content

Commit

Permalink
Add check of installed spyder-kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Aug 22, 2022
1 parent 1d8dbaf commit 402ed63
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
14 changes: 14 additions & 0 deletions spyder/plugins/ipythonconsole/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@
IPython Console plugin based on QtConsole
"""
# Required version of Spyder-kernels
SPYDER_KERNELS_MIN_VERSION = '2.3.0'
SPYDER_KERNELS_MAX_VERSION = '2.4.0'
SPYDER_KERNELS_VERSION = (
f'>={SPYDER_KERNELS_MIN_VERSION};<{SPYDER_KERNELS_MAX_VERSION}')
SPYDER_KERNELS_CONDA = (
f'conda install spyder&#45;kernels={SPYDER_KERNELS_MIN_VERSION[:-2]}')
SPYDER_KERNELS_PIP = (
f'pip install spyder&#45;kernels=={SPYDER_KERNELS_MIN_VERSION[:-1]}*')


class SpyderKernelError(RuntimeError):
"""Error to be shown in client."""
pass
43 changes: 42 additions & 1 deletion spyder/plugins/ipythonconsole/utils/kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,43 @@

# Local imports
from spyder.api.config.mixins import SpyderConfigurationAccessor
from spyder.api.translations import get_translation
from spyder.config.base import (get_safe_mode, is_pynsist, running_in_mac_app,
running_under_pytest)
from spyder.plugins.ipythonconsole import (
SPYDER_KERNELS_CONDA, SPYDER_KERNELS_PIP, SpyderKernelError)
from spyder.utils.conda import (add_quotes, get_conda_activation_script,
get_conda_env_path, is_conda_env)
from spyder.utils.environ import clean_env
from spyder.utils.misc import get_python_executable
from spyder.utils.programs import is_python_interpreter
from spyder.utils.programs import is_python_interpreter, is_module_installed

# Constants
HERE = os.path.abspath(os.path.dirname(__file__))
logger = logging.getLogger(__name__)

# Localization
_ = get_translation('spyder')
ERROR_SPYDER_KERNEL_INSTALLED = _(
"The Python environment or installation whose "
"interpreter is located at"
"<pre>"
" <tt>{0}</tt>"
"</pre>"
"doesn't have the <tt>spyder-kernels</tt> module "
"installed. Without this module is not possible for "
"Spyder to create a console for you.<br><br>"
"You can install it by activating your environment (if "
"necessary) and then running in a system terminal:"
"<pre>"
" <tt>{1}</tt>"
"</pre>"
"or"
"<pre>"
" <tt>{2}</tt>"
"</pre>"
)


def is_different_interpreter(pyexec):
"""Check that pyexec is a different interpreter from sys.executable."""
Expand Down Expand Up @@ -59,6 +84,13 @@ def get_activation_script(quote=False):
return script_path


def has_spyder_kernels(pyexec):
"""Check if env has spyder kernels."""
return is_module_installed(
'spyder_kernels',
interpreter=pyexec)


HERE = osp.dirname(os.path.realpath(__file__))


Expand Down Expand Up @@ -86,6 +118,15 @@ def argv(self):
pyexec = get_python_executable()
else:
pyexec = self.get_conf('executable', section='main_interpreter')
if not has_spyder_kernels(pyexec):
raise SpyderKernelError(
ERROR_SPYDER_KERNEL_INSTALLED.format(
pyexec,
SPYDER_KERNELS_CONDA,
SPYDER_KERNELS_PIP
)
)
return
if not is_python_interpreter(pyexec):
pyexec = get_python_executable()
self.set_conf('executable', '', section='main_interpreter')
Expand Down
1 change: 1 addition & 0 deletions spyder/plugins/ipythonconsole/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .kernelconnect import KernelConnectionDialog
from .restartdialog import ConsoleRestartDialog


# ShellWidget contains the other widgets and ClientWidget
# contains it
from .shell import ShellWidget
Expand Down
5 changes: 5 additions & 0 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from spyder.api.widgets.menus import MENU_SEPARATOR
from spyder.config.base import (
get_conf_path, get_home_dir, running_under_pytest)
from spyder.plugins.ipythonconsole import SpyderKernelError
from spyder.plugins.ipythonconsole.utils.kernelspec import SpyderKernelSpec
from spyder.plugins.ipythonconsole.utils.manager import SpyderKernelManager
from spyder.plugins.ipythonconsole.utils.client import SpyderKernelClient
Expand Down Expand Up @@ -2082,6 +2083,8 @@ def create_kernel_manager_and_kernel_client(self, connection_file,
config=None,
autorestart=True,
)
except SpyderKernelError as e:
return (e.args[0], None)
except Exception:
error_msg = _("The error is:<br><br>"
"<tt>{}</tt>").format(traceback.format_exc())
Expand All @@ -2094,6 +2097,8 @@ def create_kernel_manager_and_kernel_client(self, connection_file,
kernel_manager.start_kernel(stderr=stderr_handle,
stdout=stdout_handle,
env=kernel_spec.env)
except SpyderKernelError as e:
return (e.args[0], None)
except Exception:
error_msg = _("The error is:<br><br>"
"<tt>{}</tt>").format(traceback.format_exc())
Expand Down
28 changes: 9 additions & 19 deletions spyder/plugins/ipythonconsole/widgets/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
from spyder.plugins.ipythonconsole.utils.style import (
create_qss_style, create_style_class)
from spyder.widgets.helperwidgets import MessageCheckBox
from spyder.plugins.ipythonconsole import (
SPYDER_KERNELS_MIN_VERSION, SPYDER_KERNELS_MAX_VERSION,
SPYDER_KERNELS_VERSION, SPYDER_KERNELS_CONDA, SPYDER_KERNELS_PIP)
from spyder.plugins.ipythonconsole.comms.kernelcomm import KernelComm
from spyder.plugins.ipythonconsole.widgets import (
ControlWidget, DebuggingWidget, FigureBrowserWidget, HelpWidget,
Expand All @@ -44,40 +47,26 @@
MODULES_FAQ_URL = (
"https://docs.spyder-ide.org/5/faq.html#using-packages-installer")

# Required version of Spyder-kernels
SPYDER_KERNELS_MIN_VERSION = '2.3.0'
SPYDER_KERNELS_MAX_VERSION = '2.4.0'
SPYDER_KERNELS_VERSION = (
f'>={SPYDER_KERNELS_MIN_VERSION};<{SPYDER_KERNELS_MAX_VERSION}')
SPYDER_KERNELS_VERSION_MSG = _(
'>= {0} and < {1}').format(
SPYDER_KERNELS_MIN_VERSION, SPYDER_KERNELS_MAX_VERSION)
SPYDER_KERNELS_CONDA = (
f'conda install spyder&#45;kernels={SPYDER_KERNELS_MIN_VERSION[:-2]}')
SPYDER_KERNELS_PIP = (
f'pip install spyder&#45;kernels=={SPYDER_KERNELS_MIN_VERSION[:-1]}*')

ERROR_SPYDER_KERNEL_VERSION = _(
"The Python environment or installation whose interpreter is located at"
"<pre>"
" <tt>{0}</tt>"
"</pre>"
"doesn't have the right version of <tt>spyder-kernels</tt> installed ({1} "
"instead of {2}). Without this module is not possible for Spyder to "
"create a console for you.<br><br>"
"instead of >= {2} and < {3}). Without this module is not possible for "
"Spyder to create a console for you.<br><br>"
"You can install it by activating your environment (if necessary) and "
"then running in a system terminal:"
"<pre>"
" <tt>{3}</tt>"
" <tt>{4}</tt>"
"</pre>"
"or"
"<pre>"
" <tt>{4}</tt>"
" <tt>{5}</tt>"
"</pre>"
)



class ShellWidget(NamepaceBrowserWidget, HelpWidget, DebuggingWidget,
FigureBrowserWidget):
"""
Expand Down Expand Up @@ -379,7 +368,8 @@ def check_spyder_kernel_callback(self, reply):
ERROR_SPYDER_KERNEL_VERSION.format(
pyexec,
version,
SPYDER_KERNELS_VERSION_MSG,
SPYDER_KERNELS_MIN_VERSION,
SPYDER_KERNELS_MAX_VERSION,
SPYDER_KERNELS_CONDA,
SPYDER_KERNELS_PIP
)
Expand Down

0 comments on commit 402ed63

Please sign in to comment.