Skip to content

Commit

Permalink
Merge pull request #17788 from maurerle/feature/custom_interpreter
Browse files Browse the repository at this point in the history
PR: Unify access to current active interpreter
  • Loading branch information
ccordoba12 committed May 19, 2022
2 parents b36d120 + da69a18 commit 867e719
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def generate_python_config(self):
environment = None
env_vars = None
else:
environment = self.get_conf('custom_interpreter',
environment = self.get_conf('executable',
section='main_interpreter')
env_vars = os.environ.copy()
# external interpreter should not use internal PYTHONPATH
Expand Down
31 changes: 9 additions & 22 deletions spyder/plugins/maininterpreter/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,17 @@ def __init__(self, plugin, parent):
valid_custom_list.append(path)
self.set_option('custom_interpreters_list', valid_custom_list)

# Python executable selection (initializing default values as well)
executable = self.get_option('executable', get_python_executable())
if self.get_option('default'):
# add custom_interpreter to executable selection
executable = self.get_option('executable')

# check if the executable is valid - use Spyder's if not
if self.get_option('default') or not osp.isfile(executable):
executable = get_python_executable()
elif not self.get_option('custom_interpreter'):
self.set_option('custom_interpreter', ' ')

if not osp.isfile(executable):
# This is absolutely necessary, in case the Python interpreter
# executable has been moved since last Spyder execution (following
# a Python distribution upgrade for example)
self.set_option('executable', get_python_executable())
elif executable.endswith('pythonw.exe'):
# That should not be necessary because this case is already taken
# care of by the `get_python_executable` function but, this was
# implemented too late, so we have to fix it here too, in case
# the Python executable has already been set with pythonw.exe:
self.set_option('executable',
executable.replace("pythonw.exe", "python.exe"))

if not self.get_option('default'):
if not self.get_option('custom_interpreter'):
self.set_option('custom_interpreter', ' ')

plugin._add_to_custom_interpreters(executable)
self.validate_custom_interpreters_list()
plugin._add_to_custom_interpreters(executable)
self.validate_custom_interpreters_list()

def initialize(self):
super().initialize()
Expand Down
34 changes: 16 additions & 18 deletions spyder/plugins/maininterpreter/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,29 @@ def update_actions(self):

@on_conf_change(option=['default', 'custom_interpreter', 'custom'])
def section_conf_update(self, option, value):
if option in ['default', 'custom_interpreter', 'custom'] and value:
self._update_status()
self.sig_interpreter_changed.emit()

# Set new interpreter
if ((option == 'default' and value) or
(option == 'custom' and not value)):
executable = get_python_executable()
else:
executable = osp.normpath(self.get_conf('custom_interpreter'))
if (option in ['custom', 'custom_interpreter'] and
osp.isfile(executable)):
self.sig_add_to_custom_interpreters_requested.emit(executable)
self.sig_add_to_custom_interpreters_requested.emit(executable)

# Setting executable option that will be used by other plugins in Spyder.
if executable != self.get_conf('executable'):
self.set_conf('executable', executable)

@on_conf_change(option=['executable'])
def on_executable_changed(self, value):
# announce update
self._update_status()
self.sig_interpreter_changed.emit()

def on_close(self):
self.interpreter_status.close()

# ---- Public API
def get_main_interpreter(self):
if self.get_conf('default'):
return get_python_executable()
else:
executable = osp.normpath(self.get_conf('custom_interpreter'))

# Check if custom interpreter is still present
if osp.isfile(executable):
return executable
else:
return get_python_executable()
return self.get_conf('executable', get_python_executable())

# ---- Private API
def _update_status(self):
Expand Down
7 changes: 0 additions & 7 deletions spyder/plugins/maininterpreter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ def on_statusbar_teardown(self):
statusbar = self.get_plugin(Plugins.StatusBar)
statusbar.remove_status_widget(self.interpreter_status.ID)

# ---- Public API
def get_interpreter(self):
"""Get current interpreter."""
container = self.get_container()
return container.get_main_interpreter()

@property
def interpreter_status(self):
return self.get_container().interpreter_status
Expand All @@ -126,4 +120,3 @@ def _add_to_custom_interpreters(self, interpreter):
if interpreter not in custom_list:
custom_list.append(interpreter)
self.set_conf('custom_interpreters_list', custom_list)
self.set_conf('executable', interpreter)
5 changes: 3 additions & 2 deletions spyder/plugins/maininterpreter/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from spyder.api.translations import get_translation
from spyder.api.widgets.status import BaseTimerStatus
from spyder.utils.conda import get_list_conda_envs
from spyder.utils.misc import get_python_executable
from spyder.utils.programs import get_interpreter_info
from spyder.utils.pyenv import get_list_pyenv_envs
from spyder.utils.workers import WorkerManager
Expand Down Expand Up @@ -73,15 +74,15 @@ def get_value(self):
# Env was removed on Mac or Linux
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(sys.executable)
self.update_interpreter(get_python_executable())
elif not osp.isfile(self._interpreter):
# This can happen on Windows because the interpreter was
# renamed to .conda_trash
if not osp.isdir(osp.join(env_dir, 'conda-meta')):
# If conda-meta is missing, it means the env was removed
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(sys.executable)
self.update_interpreter(get_python_executable())
else:
# If not, it means the interpreter is being updated so
# we need to update its version
Expand Down
10 changes: 5 additions & 5 deletions spyder/plugins/profiler/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from spyder.config.base import get_conf_path
from spyder.plugins.variableexplorer.widgets.texteditor import TextEditor
from spyder.py3compat import to_text_string
from spyder.utils.misc import add_pathlist_to_PYTHONPATH, getcwd_or_home
from spyder.utils.misc import (
add_pathlist_to_PYTHONPATH, get_python_executable, getcwd_or_home)
from spyder.utils.palette import SpyderPalette, QStylePalette
from spyder.utils.programs import shell_split
from spyder.utils.qthelpers import get_item_user_text, set_item_user_text
Expand Down Expand Up @@ -560,10 +561,7 @@ def start(self, wdir=None, args=None, pythonpath=None):
if args:
p_args.extend(shell_split(args))

executable = sys.executable
if executable.endswith("spyder.exe"):
# py2exe distribution
executable = "python.exe"
executable = self.get_conf('executable', section='main_interpreter')

self.process.start(executable, p_args)
running = self.process.waitForStarted()
Expand Down Expand Up @@ -1040,6 +1038,8 @@ def test():
widget = ProfilerWidget('test', plugin=plugin_mock)
widget._setup()
widget.setup()
widget.get_conf('executable', get_python_executable(),
section='main_interpreter')
widget.resize(800, 600)
widget.show()
widget.analyze(script)
Expand Down
2 changes: 1 addition & 1 deletion spyder/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_error_match(text):


def get_python_executable():
"""Return path to Python executable"""
"""Return path to Spyder Python executable"""
executable = sys.executable.replace("pythonw.exe", "python.exe")
if executable.endswith("spyder.exe"):
# py2exe distribution
Expand Down

0 comments on commit 867e719

Please sign in to comment.